C# WPF MVVM依赖项属性获取负值
我有两个按钮,发送和取消发送。我想使用布尔值C# WPF MVVM依赖项属性获取负值,c#,wpf,mvvm,C#,Wpf,Mvvm,我有两个按钮,发送和取消发送。我想使用布尔值issend启用或禁用按钮 我已经创建了一个可用于启用其中一个按钮的dependency属性,但是如何使用负值来控制另一个按钮呢 看法 视图模型 public Boolean IsSent { get { return (Boolean) GetValue(IsSendedProperty); } set { SetValue(IsSendedProperty, value); } } public static readonly Dep
issend
启用或禁用按钮
我已经创建了一个可用于启用其中一个按钮的dependency属性,但是如何使用负值来控制另一个按钮呢
看法
视图模型
public Boolean IsSent
{
get { return (Boolean) GetValue(IsSendedProperty); }
set { SetValue(IsSendedProperty, value); }
}
public static readonly DependencyProperty IsSendedProperty = DependencyProperty.Register("IsSent", typeof(Boolean), typeof(ViewModel), new PropertyMetadata(default(Boolean)));
在WPF中有许多was是这样做的<代码>IValueConverters,
数据模板
或覆盖控制模板
很难知道从长远来看,对于您的特定应用程序,哪一个是最好的,但这里要展示的最简单的是IValueConverter
添加一个名为的类,例如,NegateBoolConverter
public class NegateBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value;
}
}
在XAML中创建这个类的实例(我把我所有的转换器都放在App.XAML
中,但是你可以把它放在
部分
<Window.Resources>
<local:NegateBoolConverter x:Key="MyConverter"/>
</Window.Resources>
不确定为什么要引入
dependencProperty
。它可以是bool issend
属性,您可以使用它绑定以否定属性。
<Window.Resources>
<local:NegateBoolConverter x:Key="MyConverter"/>
</Window.Resources>
<dxb:BarButtonItem Content="SEND" IsEnabled="{Binding IsSended, Converter={StaticResource MyConverter}}">