zoukankan      html  css  js  c++  java
  • 关于ObjectDataProvider绑定方法使用案例

    http://blog.csdn.net/iamsupercola/article/details/7050709

    这个案例实现什么功能:ComboBox是个组合框,即下拉菜单,对此实现数据绑定。

    首先新建了EnumType.cs,定义了一个枚举InlineToolType,

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace SQLtest.Model
    {
        public enum InlineToolType
        {
            Noraml,
            Sorter,
            CassetteCleaner,
            MaskCleaner,
            BufferUsing,
            NoUseTransfer,
            Packing,
            CoverLens,
            CP,
            CFOG
        }
    }

    #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    // C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5mscorlib.dll

    #endregion

    在 mscorlib ,命名空间 namespace System中有一个函数  public static Array GetValues(Type enumType);

    函数简介:获取枚举的全部元素

    // 参数:
    // enumType:
    // An enumeration type.
    // 返回结果:
    // An array that contains the values of the constants in enumType.

    首先是WPF端的关键代码,命名空间的引用,一个是上面提到的GetValues函数的命名空间,包括程序集

                 xmlns:sys="clr-namespace:System;assembly=mscorlib"
                 xmlns:u="clr-namespace:SQLtest.Model"  

    ObjectDataProvider数据绑定:在此X:key=“InlineTypeEnum”  方法名称是GetValues,    类型是枚举  ObjectType="{x:Type sys:Enum}",参数类型“u:InlineToolType”

    在XAML文件中,我们可以把需要多次使用的类容提取出来放在资源字典中,需要使用的时候就用这个资源的key将这个资源检索出来。

    x:key的作用就是使用为资源贴上用于检索的索引。在WPF中,几乎每个元素都有自己的Resource属性,这个属性就是“key-value”的集合。只要把元素放进这个集合里,这个元素就成了资源字典中的一个条目。当然,为了能检索到这个条件,就必须为它添加x:Key。资源在WPF中非常重要,需要重复使用的XAML内容,如Style,各种Template和动画都需要放在资源里。
     
    1    <UserControl.Resources >
    2         <ObjectDataProvider x:Key="InlineTypeEnum" MethodName="GetValues"  ObjectType="{x:Type sys:Enum}" >
    3             <ObjectDataProvider.MethodParameters>
    4                 <x:Type TypeName="u:InlineToolType"/>
    5             </ObjectDataProvider.MethodParameters>
    6         </ObjectDataProvider>
    7     </UserControl.Resources>

    和Combobox的绑定,绑定静态资源InlineTypeEnum,通过key查找到GetValues方法得到的InlineToolType 资源,加载到ComboBox中。

    1             <ComboBox  Width="100" 
    2                        ItemsSource="{Binding Source={StaticResource InlineTypeEnum}}" 
    3                        SelectedItem="{Binding SelectedInlineToolType}" >
    4             </ComboBox>
    但愿人长久 千里共婵娟
  • 相关阅读:
    xcode创建多个target,老外的写的懒得翻译了,看图
    错误 解决“Unknown class in Interface Builder file”
    Objectc 动态调用函数
    IPA PNG图片转换
    ObjectiveC的消息传递机制[转]
    [转]获取iOS设备的内存状况
    [转]史上最简单得多选uitableview
    Unity3d 添加IOS View(2)
    Core Animation学习笔记五:CAPropertyAnimation
    MKMapView指定坐标添加大头针
  • 原文地址:https://www.cnblogs.com/hellcats/p/6015826.html
Copyright © 2011-2022 走看看