在这种情况下,您最好使用DataTemplate
而不是Converter
。
您已拥有数据类。只需使用DataTemplate
插入绑定到int值的Textblock
,然后在那里应用您的转换器。
<ComboBox>
<ComboBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:TaxDataSet.SourcesOfValuesRow}">
<TextBlock Text="{Binding FamilyStatus, Converter={StaticResource FamilyStatusStringConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox>
将您的SourcesOfValuesRow FamilyStatusProperty更改为枚举。从int派生让你直接施放它。
enum FamilyStatusValues : int
{
[Description("Married")]
Married,
[Description("Divorced")]
Divorced,
[Description("Living Together")]
LivingTogether
}
然后在你的转换器使用此代码
ConvertTo(object value, ...)
{
FieldInfo field = value.GetType().GetField(value.ToString());
object[] attribs = field.GetCustomAttributes(typeof(DescriptionAttribute), true));
if(attribs.Length > 0)
{
return ((DescriptionAttribute)attribs[0]).Description;
}
return string.Empty;
}
您使用“EnumerableRowCollection”而不是“字典”的任何特定原因?我知道如果你使用'Dictionary ',你可以使用'SelectedValuePath =“Key绑定”DisplayMemberPath =“Value”' –
2013-04-04 19:32:26