public class BoolToOppositeBoolConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (targetType != typeof(bool))
throw new InvalidOperationException("The target must be a boolean");
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
3 回答
使用样式触发器:
从IValueConverter派生的类应该可以解决这个问题:
最好在资源字典中,创建转换器的实例:
然后在您的视图中,执行类似这样的操作,其中IsChecked的bool值将转换为相反的值 . 不要忘记将资源字典包含为视图的资源 .
另一种方法是使用 Blend 的
DataTrigger
: