WPF Grid.IsSharedSizeScope 跨多个网格
WPF Grid.IsSharedSizeScope across multiple grids
我遇到这样一种情况,我的按钮位于多个网格中,并且要求所有按钮的大小都相同。我尝试使用 Grid.IsSharedSizeScope 但没有成功。
最终布局应如下图所示,但所有按钮的大小应相同。
XAML目前看起来是这样的。有人看到我哪里出错了吗?
<UserControl x:Class="UserControls.UserControl2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Grid.IsSharedSizeScope="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<GroupBox Grid.Row="0" Grid.Column="0" Header="Header 1" Grid.IsSharedSizeScope="True">
<Grid Grid.IsSharedSizeScope="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
</Grid.ColumnDefinitions>
<Button Content="A" Grid.Row="0" Grid.Column="0" Margin="2" />
<Button Content="B" Grid.Row="0" Grid.Column="1" Margin="2" />
<Button Content="C" Grid.Row="0" Grid.Column="2" Margin="2" />
</Grid>
</GroupBox>
<GroupBox Grid.Row="1" Grid.Column="0" Header="Header 2" Grid.IsSharedSizeScope="True">
<Grid Grid.IsSharedSizeScope="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
</Grid.ColumnDefinitions>
<Button Content="AA" Grid.Row="0" Grid.Column="0" Margin="2" />
<Button Content="BB" Grid.Row="0" Grid.Column="1" Margin="2" />
</Grid>
</GroupBox>
</Grid>
仅将 Grid.IsSharedSizeScope 应用于顶级容器。参考下面的代码。
<Grid Grid.IsSharedSizeScope="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<GroupBox Grid.Row="0" Grid.Column="0" Header="Header 1" >
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
</Grid.ColumnDefinitions>
<Button Content="A" Grid.Row="0" Grid.Column="0" Margin="2" />
<Button Content="B" Grid.Row="0" Grid.Column="1" Margin="2" />
<Button Content="C" Grid.Row="0" Grid.Column="2" Margin="2" />
</Grid>
</GroupBox>
<GroupBox Grid.Row="1" Grid.Column="0" Header="Header 2" >
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
</Grid.ColumnDefinitions>
<Button Content="AA" Grid.Row="0" Grid.Column="0" Margin="2" />
<Button Content="BB" Grid.Row="0" Grid.Column="1" Margin="2" />
</Grid>
</GroupBox>
</Grid>
我遇到这样一种情况,我的按钮位于多个网格中,并且要求所有按钮的大小都相同。我尝试使用 Grid.IsSharedSizeScope 但没有成功。
最终布局应如下图所示,但所有按钮的大小应相同。
XAML目前看起来是这样的。有人看到我哪里出错了吗?
<UserControl x:Class="UserControls.UserControl2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Grid.IsSharedSizeScope="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<GroupBox Grid.Row="0" Grid.Column="0" Header="Header 1" Grid.IsSharedSizeScope="True">
<Grid Grid.IsSharedSizeScope="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
</Grid.ColumnDefinitions>
<Button Content="A" Grid.Row="0" Grid.Column="0" Margin="2" />
<Button Content="B" Grid.Row="0" Grid.Column="1" Margin="2" />
<Button Content="C" Grid.Row="0" Grid.Column="2" Margin="2" />
</Grid>
</GroupBox>
<GroupBox Grid.Row="1" Grid.Column="0" Header="Header 2" Grid.IsSharedSizeScope="True">
<Grid Grid.IsSharedSizeScope="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
</Grid.ColumnDefinitions>
<Button Content="AA" Grid.Row="0" Grid.Column="0" Margin="2" />
<Button Content="BB" Grid.Row="0" Grid.Column="1" Margin="2" />
</Grid>
</GroupBox>
</Grid>
仅将 Grid.IsSharedSizeScope 应用于顶级容器。参考下面的代码。
<Grid Grid.IsSharedSizeScope="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<GroupBox Grid.Row="0" Grid.Column="0" Header="Header 1" >
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
</Grid.ColumnDefinitions>
<Button Content="A" Grid.Row="0" Grid.Column="0" Margin="2" />
<Button Content="B" Grid.Row="0" Grid.Column="1" Margin="2" />
<Button Content="C" Grid.Row="0" Grid.Column="2" Margin="2" />
</Grid>
</GroupBox>
<GroupBox Grid.Row="1" Grid.Column="0" Header="Header 2" >
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
</Grid.ColumnDefinitions>
<Button Content="AA" Grid.Row="0" Grid.Column="0" Margin="2" />
<Button Content="BB" Grid.Row="0" Grid.Column="1" Margin="2" />
</Grid>
</GroupBox>
</Grid>