除了@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
}
}