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 Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.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將這個資源檢索出來。
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>