您的位置:首页 > 其它

Enum Binding ItemsSource In WPF

2011-08-17 23:24 405 查看
在WPF中枚举绑定到ItemsSource。

一、通过ObjectDataProvider 获取Enum数据源

首先我们定义一个Enum类:

 public enum TableSelectedType


[code] {


SelectedOne,


 


SelectedTwo,


 


SelectedThird


}

[/code]

接着在Xaml中的Resource里定义数据源。

<UserControl.Resources>


[code] <ObjectDataProvider x:Key="odp" MethodName="GetNames" ObjectType="{x:Type System:Enum}">


<ObjectDataProvider.MethodParameters>


<x:Type TypeName="local:TableSelectedType"/>


</ObjectDataProvider.MethodParameters>


</ObjectDataProvider>


</UserControl.Resources>

[/code]

我们这里写好了一个Enum数据源,他的key是odp。我们把它绑定到ComboBox的ItemsSource上看下。

<ComboBox ItemsSource="{Binding Source={StaticResource odp}}"/>



效果图:

代码 <ComboBox ItemsSource="{Binding Source={StaticResource odp}, Converter={StaticResource EnumTypeToStringConverter}}"/>


效果:





二、通过Dictionary来存Enum,并绑定到ItemsSource上

这种方便且效率高。我们还是用上面的Enum类型。我们声明一个Dictionary的属性TableSelectedTypeCollection ,并对他初始话。

 public partial class ConboBoxEnum : UserControl


[code] {


public ConboBoxEnum()


{


InitializeComponent();


 


TableSelectedTypeCollection=new Dictionary<TableSelectedType, string>();


TableSelectedTypeCollection.Add(TableSelectedType.SelectedOne,"第一条");


TableSelectedTypeCollection.Add(TableSelectedType.SelectedTwo,"第二条");


TableSelectedTypeCollection.Add(TableSelectedType.SelectedThird,"第三条");


this.DataContext = this;


}


 


public Dictionary<TableSelectedType, string> TableSelectedTypeCollection { get; set; }


}

[/code]

Xaml中,我们用TableSelectedTypeCollection 来绑定ComboBox的ItemsSource。

<ComboBox ItemsSource="{Binding TableSelectedTypeCollection}" SelectedValue="{Binding TableSelectedType}" DisplayMemberPath="Value"/>


这里我们用Value来显示它对应的中文字。SelectedValue来绑定它的Enum类型。因为后台我们通常用Enum中的类型。

效果还是一样。





三、通过特性(Attribute)来获取

这里用到了MS命名空间下的using System.ComponentModel;在枚举元素上加Description这个特性。

public enum TableSelectedType
{
[Description("选择第一行")]
SelectedOne,
[Description("选择第二行")]
SelectedTwo,
[Description("选择第三行")]
SelectedThird
}


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

我们写一个EnumHelper来获取它。

public static class EnumHelper
{
public static T GetEnumAttribute<T>(Enum source)where T:Attribute
{
Type type = source.GetType();
var sourceName = Enum.GetName(type, source);
FieldInfo field = type.GetField(sourceName);
object[] attributes = field.GetCustomAttributes(typeof (T), false);
foreach (var o in attributes)
{
if (o is T)
return (T) o;
}
return null;
}

public static string GetDescription(Enum source)
{
var str = GetEnumAttribute<DescriptionAttribute>(source);
if (str==null)
return null;
return str.Description;
}

}


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

然后我们在实例化这个枚举的时候,调用它就可以。
public Dictionary<TableSelectedType, string> TableSelectedTypeDictionary { get; set; }
public ConboBoxEnum()
{
InitializeComponent();

TableSelectedTypeDictionary=new Dictionary<TableSelectedType, string>();
TableSelectedTypeDictionary.Add(TableSelectedType.SelectedOne, EnumHelper.GetDescription(TableSelectedType.SelectedOne));
TableSelectedTypeDictionary.Add(TableSelectedType.SelectedTwo, EnumHelper.GetDescription(TableSelectedType.SelectedTwo));
TableSelectedTypeDictionary.Add(TableSelectedType.SelectedThird, EnumHelper.GetDescription(TableSelectedType.SelectedThird));

this.DataContext = this;
}


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: