• WPF当中StaticResource调用方法


    1.先在Converter命名空间当中,定义转换功能类:

    public sealed class BoolToValueConverter : System.Windows.Data.IValueConverter

        {
            public object TrueValue
            {
                get { return trueValue; }
                set { trueValue = value; }
            }
            object trueValue;
            public object FalseValue
            {
                get { return falseValue; }
                set { falseValue = value; }
            }
            object falseValue;

            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (value == null)
                    return DependencyProperty.UnsetValue;
                var data = value.GetType().Name;
                if (value.GetType().Name.ToLower().Contains("int")|| value.GetType().Name == "Decimal")
                {
                    if (int.Parse(value.ToString()) !=0)
                    {
                        return TrueValue;
                    }
                    else
                    {
                        return FalseValue;
                    }
                }
                if (bool.TryParse(value.ToString(), out bool
                    result))
                {
                    if (result)
                        return TrueValue;
                    else
                        return FalseValue;
                }
                else if (Enum.TryParse(value.ToString(), out Visibility
                     Enumresult))
                {
                    if (Enumresult == Visibility.Visible)
                        return TrueValue;
                    else
                        return FalseValue;
                }
                else
                {
                    return DependencyProperty.UnsetValue;
                }
            }
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return DependencyProperty.UnsetValue;
            }
        }
     
    2.引用命名空间
              xmlns:converter="clr-namespace:Converter"
     
    3.使用方法:
              在ResourceDictionary当中调用:
              <converter:BoolToValueConverter x:Key="BoolToColorConverter" TrueValue="#3F48CC" FalseValue="#ED1C24"/>
     
              <TextBlock TextWrapping="Wrap" Grid.Column="1" Text="{Binding IsSensitiveArea,Converter={StaticResource BoolToColorConverter}}"/>
     
              在window当中定义和调用:       
        <window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/Infrastructure;componentStyleThemesStyle.xaml" />
                </ResourceDictionary.MergedDictionaries>
                <converter:BoolToValueConverter x:Key="BoolToValueConverterOnWord" TrueValue="是" FalseValue="否"/>
            </ResourceDictionary>
        </window.Resources>
        
         <TextBlock TextWrapping="Wrap" Grid.Column="1" Text="{Binding IsSensitiveArea,Converter={StaticResource BoolToValueConverterOnWord}}"/>
     
  • 相关阅读:
    uni-app 调用支付宝支付
    PLSQL安装教程,无需oracle客户端(解决本地需要安装oracle客户端的烦恼)
    Java 基于WebMagic 开发的网络爬虫
    android 手机权限管理——PermissionsDispatcher
    Eclipse 上传 删除 下载 分析 hdfs 上的文件
    初见Hadoop—- 搭建MyEclipse 访问HDFS 上的文件
    仿简书登录框,可删除内容或显示密码框的内容
    setFeatureInt、android 自定义标题栏
    Translucent Bar Android状态栏自定义颜色
    仿QQ消息气泡提醒
  • 原文地址:https://www.cnblogs.com/wlming/p/10070933.html
走看看 - 开发者的网上家园