WPF绑定按钮

WPF绑定按钮,wpf,binding,Wpf,Binding,我的应用程序中有两个按钮 现在我想把btnOff绑定到!伊森。意思是btnOn被启用,btnOff应该被禁用,反之亦然 编辑:以下是我的实现: <Button x:Name="btnOn" Content="On" Width="45" Height="24" IsEnabled="{Binding isOn, Converter={StaticResource BoolInverter}}" /> <Button x:Name="btnOff" Content="O

我的应用程序中有两个按钮

现在我想把btnOff绑定到!伊森。意思是btnOn被启用,btnOff应该被禁用,反之亦然

编辑:以下是我的实现:

<Button x:Name="btnOn" Content="On" Width="45" Height="24" IsEnabled="{Binding isOn, Converter={StaticResource BoolInverter}}" />
   <Button x:Name="btnOff" Content="Off" Width="45" Height="24" IsEnabled="{Binding isOn} />

 public class BoolInverterConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                return !(bool)value;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                return !(bool)value;
            }
            return value;
        }

    }
,


->元素数据绑定的另一个示例

使用另一个计算/派生属性Isof no backing字段否定IsOn属性不是更简单吗

public bool IsOn{ 
  get{...}
  set
  {    _isOn = value; 
       NotifyPropertyChanged("IsOn");
       NotifyPropertyChanged("IsOff");
  }
}

public bool IsOff
{
   get{   return !IsOn;}
}

转换器通常用于转换数据类型,例如,将可见性枚举属性绑定到viewmodel中的布尔备份属性。

到目前为止,您有什么经验?我实现了转换器以反转布尔值,现在可以正常工作。