zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    View Code
        <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>
    View Code

    这两者的使用区别则是绑定的值有没有null的情况,有null的情况绑定devexpress类型的BoolToObjectConverter则会显示无效bool值转换。但没有null的情况下,两者是都可以绑定的。

    例如某个控件的IsEnabled属性只有true和false则可以绑定devexpress类型的BoolToObjectConverter

     
     
    但愿人长久 千里共婵娟
  • 相关阅读:
    程序员,你有多久没关爱自己了?
    如何优化 Java 性能?
    想让安卓 APP 如丝般顺滑?
    用 OneAPM Cloud Insight 监控 Docker 性能
    盘点 OSX 上最佳的 DevOps 工具
    荣誉,还是苦逼?| 也议全栈工程师和DevOps
    小程序基础知识点讲解-WXML + WXSS + JS,生命周期
    第二十一节:Java语言基础-关键字,标识符,注释,常量和变量,运算符
    第二十一节:Java语言基础-关键字,标识符,注释,常量和变量,运算符
    第二十一节:Java语言基础-关键字,标识符,注释,常量和变量,运算符
  • 原文地址:https://www.cnblogs.com/hellcats/p/8662215.html
Copyright © 2011-2022 走看看