假设您有一个带有多个按钮的窗口,例如确定/取消或是/否/取消。所有按钮的宽度必须相同。显然,这可以通过猜测一个数字并将它们全部硬连接到该数字来完成。
有没有更好的方法来做到这一点,一种会考虑首选/推荐尺寸的方法(“确定”按钮应该有多宽?这不是一个反问句,我实际上不知道答案!),需要什么根据最长标题的文本,如果字体大小增加等会发生什么?
另一种也许更简单的方法是使用SharedSizeGroup财产在ColumnDefinition and RowDefinition类。
WPF 网格中的列(和行)可以自动调整大小以适合其内容 - 当SharedSizeGroup
使用时,具有相同组名的列共享其大小调整逻辑。
Xaml 看起来像这样......
<Grid Grid.IsSharedSizeScope="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition SharedSizeGroup="Buttons" />
<ColumnDefinition SharedSizeGroup="Buttons" />
<ColumnDefinition SharedSizeGroup="Buttons" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Column="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Content="Ok"
Margin="4" />
<Button Grid.Column="2"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Content="Cancel"
Margin="4" />
<Button Grid.Column="3"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Content="Long Button Caption"
Margin="4" />
</Grid>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)