【发布时间】:2022-01-15 11:38:54
【问题描述】:
我在使用枚举作为 xaml 文件中的键绑定到字典属性时遇到了问题。
我有一个带有属性的 ViewModel
public Dictionary<EColor, MyDataStatus> StatusByColor {get; set;}
EColor 是枚举,MyDataStatus 是我自己的具有属性 Value 的类。
在视图中我正在尝试创建绑定
<TextBlock Text="{Binding StatusByColor[enum:EColor.eRed].Value}"/>
我得到一个绑定错误
BindingExpression path error: '[]' property not found on 'object' ''Dictionary`2 .....
但是,如果我像这样输入一个数字(而不是使用枚举):
<TextBlock Text="{Binding StatusByColor[0].Value}"/>
效果很好
我认为这可能是 xaml 中“包含”枚举的问题(它在其他程序集中定义)但下面几行我可以像这样将它用作 CommandParameter。
Text="{Binding Path=StatusByColor, Mode=OneWay,
Converter={StaticResource statusByColorToDayElapsedConverter},
ConverterParameter={x:Static enum:EPaintColor.eRed}}"
在这种情况下(使用转换器参数),甚至 IntelliSense 都会建议枚举的值。
这就是我在 xaml 中“包含”枚举的方式:
"xmlns:enum="clr-namespace:CommonTypes.Enums;assembly=CommonTypes""
【问题讨论】:
-
这能回答你的问题吗? Using an enum as an array index in C#
-
恐怕不是 :( 或者我不明白如何使用答案。
标签: wpf dictionary enums binding