• [原创]WPF资源Binding自定义集合类。


    简单介绍一下Wpf资源字典:

    每个WPF界面元素都有一个名为Resource的属性,这个属性继承至FrameworkElement类,其类型为ResourceDictionary。ResourceDictionary能够以键值对的形式存储资源,当要使用到某个资源的时候,使用键值对的形式获取资源对象。在保存资源时,ResourceDictionary视资源对象为Object类型,所以再使用资源时先要对资源对象进行类型转换,XAML编译器能够根据Attribute自动识别资源类型,如果类型不对就会抛出异常。

    如果资源字典中存储的是集合类型,而应用时只想取其中一个元素来绑定,这样就需要自己编写转换器,来返回需要的元素值。

    下面演示绑定集合中某元素例子:

    首先定义集合内容:

     1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     2                     xmlns:sys="clr-namespace:System;assembly=mscorlib"
     3                     xmlns:syscollection="clr-namespace:System.Collections;assembly=mscorlib"
     4                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
     5 <!--登录界面-->
     6     <syscollection:ArrayList x:Key="page_login">
     7         <syscollection:DictionaryEntry Key="title" Value="系统"/>
     8         <syscollection:DictionaryEntry Key="login" Value="登录"/>
     9         <!--提示-->
    10         <syscollection:DictionaryEntry Key="user_isnull" Value="用户不能为空"/>
    11         <syscollection:DictionaryEntry Key="password_isnull" Value="密码不能为空"/>
    12         <syscollection:DictionaryEntry Key="user_noexist" Value="用户不存在"/>
    13         <syscollection:DictionaryEntry Key="password_error" Value="密码错误"/>
    14     </syscollection:ArrayList>
    15 
    16 </ResourceDictionary>

    其次定义转换器:

     1 class CultureConverter : IValueConverter
     2     {
     3         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     4         {
     5             if (value == null) return null;
     6             ArrayList lst = value as ArrayList;
     7             if (lst == null) return null;
     8             Dictionary<object, object> dic = lst.Cast<DictionaryEntry>().ToDictionary(item => item.Key, item => item.Value);
     9             if (dic.ContainsKey(parameter))
    10                 return dic[parameter];
    11             else
    12                 return null;
    13         }
    14 
    15         public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    16         {
    17             throw new NotImplementedException();
    18         }
    19     }

    最后在Xaml中引用资源:

    1     <!--定义转换器资源-->
    2     <Window.Resources>
    3         <local:CultureConverter x:Key="CultureConverter"/>
    4     </Window.Resources>
    5   <!--在Xaml中引用资源-->
    6 <Label  Content="{Binding Converter={StaticResource CultureConverter}, ConverterParameter=title,Source={StaticResource page_login}}"  Grid.Row="1"  VerticalAlignment="Top" FontSize="36" FontWeight="Bold" Padding="0" Grid.ColumnSpan="2" HorizontalContentAlignment="Center" />

    注意:在Converter和Source中不可以引用DynamicResource。

    应用资源转换器可以灵活的实现资源的引用,尤其是分组资源。一个非常好的案例:国际化的应用。

  • 相关阅读:
    bzoj 2763: [JLOI2011]飞行路线
    bzoj 2761: [JLOI2011]不重复数字
    bzoj 2744: [HEOI2012]朋友圈
    bzoj 2743: [HEOI2012]采花
    bzoj 2730: [HNOI2012]矿场搭建
    bzoj 2705: [SDOI2012]Longge的问题
    抗DDOS攻击
    kali安装后配置
    Kali更新源,亲测目前可用的源
    kali安装及配置ssr客户端
  • 原文地址:https://www.cnblogs.com/hehexiaoxia/p/5685617.html
Copyright © 2020-2023  润新知