BoolToObjectConverter
介绍两种BoolToObjectConverter,一种是自定义的,一个是devexpress的
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:com="http://schemas.Sample.com.cn/common"
public class BoolToObjectConverter : DependencyObject, IValueConverter { public static readonly DependencyProperty TrueValueProperty = DependencyProperty.RegisterAttached(nameof(TrueValue), typeof(object), typeof(BoolToObjectConverter)); public static readonly DependencyProperty FalseValueProperty = DependencyProperty.RegisterAttached(nameof(FalseValue), typeof(object), typeof(BoolToObjectConverter)); public static readonly DependencyProperty NullValueProperty = DependencyProperty.RegisterAttached(nameof(NullValue), typeof(object), typeof(BoolToObjectConverter)); public object NullValue { get { return GetValue(NullValueProperty); } set { SetValue(NullValueProperty, value); } } public object TrueValue { get { return GetValue(TrueValueProperty); } set { SetValue(TrueValueProperty, value); } } public object FalseValue { get { return GetValue(FalseValueProperty); } set { SetValue(FalseValueProperty, value); } } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if(value == null || (!(value is bool?) && !(value is bool))) { return NullValue; } return ((bool)value) ? TrueValue : FalseValue; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } }
<UserControl.Resources> <com:BoolToObjectConverter x:Key="wrapIconSelector"> <com:BoolToObjectConverter.TrueValue> <BitmapImage UriSource="pack://application:,,,/Sineva.Modeler.Define;component/image/word-wrap-on.png" /> </com:BoolToObjectConverter.TrueValue> <com:BoolToObjectConverter.NullValue> <BitmapImage UriSource="pack://application:,,,/Sineva.Modeler.Define;component/image/word-wrap-off.png" /> </com:BoolToObjectConverter.NullValue> <com:BoolToObjectConverter.FalseValue> <BitmapImage UriSource="pack://application:,,,/Sineva.Modeler.Define;component/image/word-wrap-off.png" /> </com:BoolToObjectConverter.FalseValue> </com:BoolToObjectConverter> <com:BoolToObjectConverter x:Key="actionStateIconSelector"> <com:BoolToObjectConverter.TrueValue> <BitmapImage UriSource="pack://application:,,,/Sineva.Modeler.Define;component/image/mini_ok.png" /> </com:BoolToObjectConverter.TrueValue> <com:BoolToObjectConverter.NullValue> <BitmapImage UriSource="pack://application:,,,/Sineva.Modeler.Define;component/image/mini_error.png" /> </com:BoolToObjectConverter.NullValue> <com:BoolToObjectConverter.FalseValue> <BitmapImage UriSource="pack://application:,,,/Sineva.Modeler.Define;component/image/mini_error.png" /> </com:BoolToObjectConverter.FalseValue> </com:BoolToObjectConverter> <dx:BoolToObjectConverter x:Key="ActionStateIconSelector"> <dx:BoolToObjectConverter.TrueValue> <Visibility>Visible</Visibility> </dx:BoolToObjectConverter.TrueValue> <dx:BoolToObjectConverter.FalseValue> <Visibility>Hidden</Visibility> </dx:BoolToObjectConverter.FalseValue> </dx:BoolToObjectConverter> </UserControl.Resources>
这两者的使用区别则是绑定的值有没有null的情况,有null的情况绑定devexpress类型的BoolToObjectConverter则会显示无效bool值转换。但没有null的情况下,两者是都可以绑定的。
例如某个控件的IsEnabled属性只有true和false则可以绑定devexpress类型的BoolToObjectConverter
但愿人长久
千里共婵娟