我有两个按钮,发送和取消发送。我想使用 bool 值 IsSended
启用或禁用按钮.
我创建了一个依赖属性,可用于启用其中一个按钮,但如何取负值来控制另一个按钮?
看法
dxb:BarButtonItem Content="SEND" IsEnabled="{Binding !IsSent}"
dxb:BarButtonItem Content="UNSEND" IsEnabled="{Binding IsSent}"
View 模型
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 中有很多方法可以做到这一点; IValueConverters
, DataTemplate
或覆盖 ControlTemplate
.
从长远来看,对于您的特定应用程序来说,很难知道哪个是最好的,但这里最简单的展示是 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>
部分。 <Window.Resources>
<local:NegateBoolConverter x:Key="MyConverter"/>
</Window.Resources>
在哪里
local:
是转换器类的命名空间然后你的绑定(bind)变成:
<dxb:BarButtonItem Content="SEND" IsEnabled="{Binding IsSended, Converter={StaticResource MyConverter}}">
关于c# - WPF MVVM依赖属性获取负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35278023/