SliverLight注册字典转换器方法

SliverLight注册资源——本处以添加字典转换器为列。

该列子中,是动态添加DataGrid复杂表头,而数据源是字典集合,因此使用传统的数据绑定就不能(个人能力)实现。

添加表头并绑定数据代码如下。

其中ColumnItem是找的一个dll中的方法,该dll主要就是为了动态创建复杂表头而生,此处不做过多解释。第一个参数是表头名称,第二个参数是数据字典Key,第三个参数是字典转换器,第四个参数是数据显示位置,第五个参数是列宽。

如果不绑定第三个参数,则不能正常显示数据,显示情况如下:

这里重点描述第三个参数的由来:dicCommonValueConverter是字典转换器注册别名。

字典转换器的类:

public class DictionaryCommonValueConverter : System.Windows.Data.IValueConverter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            { 
                return null;
            }
            IDictionary<string, object> dic = value as IDictionary<string, object>;
            if (dic == null)
            {
                return null;
            }
            object dicValue;
            if (dic.TryGetValue(parameter.ToString(), out dicValue))
            {
                return dicValue;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("不支持的转换。");
        }
    }
字典转换器必须实现IValueConverter接口,可在网上查阅。
接下来用一.xaml文件注册此字典转换器:
<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
  xmlns:convert="clr-namespace:Xunmei.Web.ShsdbManager.DataConverter" //转换器类空间名称
    >
   <!-- ******注册转换器****** -->
    <convert:DictionaryCommonValueConverter x:Key="dicCommonValueConverter" />//类名和别名
</ResourceDictionary>

使用这个注册方式有三种:

第一种是在App.xaml中执行

<Application 
    x:Class="Xunmei.Web.ShsdbManager.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Application.Resources>
        <!-- 应该在此定义应用程序级的资源。-->
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="resources/styles/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
<ResourceDictionary Source="resources/styles/Styles.xaml"/>是指注册文件Styles.xaml的路径。

但是此方法的使用前提下是程序运行先要走这个App文件,不然也不能注册。

第三种方式是在界面上注册:

如果是这个注册方法,就可以不适用Styles.xaml文件,直接使用字典转换器类文件。但是这个种方法也有局限性,只能在该前台界面使用,不能再后台使用。

第三种方法是在后台注册:

ResourceDictionary dicCommonValueConverter = new ResourceDictionary();
Application.LoadComponent(dicCommonValueConverter, new Uri("/Xunmei.Web.ShsdbManager.ShsdbManagerSL;component/Styles.xaml", UriKind.Relative));
Application.Current.Resources.MergedDictionaries.Add(dicCommonValueConverter);  

在后台注册后就可以直接在后台绑定字典集合类型的数据。

因为本次案例用的是第三个方法。最终数据正常显示:

 

 

posted @ 2016-06-05 10:47  小丿悠悠  阅读(183)  评论(0编辑  收藏  举报