使两个按钮有相同的宽度
本文关键字:按钮 两个 | 更新日期: 2023-09-27 17:50:05
我有以下内容:
<DockPanel Height="25" HorizontalAlignment="Stretch">
<Button Content="Add" x:Name="bAdd" DockPanel.Dock="Left" />
<Button Content="Remove" x:Name="bRemove" DockPanel.Dock="Right" />
</DockPanel>
有人能建议我如何使两个按钮有相等的宽度,而不手动设置按钮的Width
属性?
如果你绝对不想设置Width
属性,你可以使用Grid
:
<Grid Height="25" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="Add" x:Name="bAdd" />
<Button Grid.Column="1" Content="Remove" x:Name="bRemove" />
</Grid>
它们都有相同的Width
<UniformGrid Columns="2">
<Button Content="Ok" Grid.Column="0"/>
<Button Content="Cancel" Grid.Column="1"/>
</UniformGrid>
除了@Amir的答案,你还可以通过绑定可见性来动态控制按钮的数量。
<UniformGrid Rows="1">
<Button Content="Ok" Visiblity="{Binding IsShowOkayButton, Converter={...}"/>
<Button Content="Cancel" Visiblity="{Binding IsShowCancelButton, Converter={...}}"/>
</UniformGrid>
与枚举一起使用:(假设ConfirmType和IsShowCancelButton在同一个ViewModel中)
public enum ConfirmType { Confirm, ConfirmOrCancel }
public ConfirmType ConfirmType { get => _ConfirmType;
set {
switch (_ConfirmType)
{
case ConfirmType.Confirm:
IsShowCancelButton = false;
break;
case ConfirmType.ConfirmOrCancel:
IsShowCancelButton = true;
break;
}
// Your RaisePropertyChanged code here
}
}