[英]WPF data binding to dictionary with enum key
I have a solution with a couple of projects. 我有几个项目的解决方案。 In one project, is my model that has an enum called ModelEnum.
在一个项目中,我的模型有一个名为ModelEnum的枚举。
Then in my WPF project I have a ViewModel which has a Dictionary. 然后在我的WPF项目中,我有一个带有字典的ViewModel。
And in my ViewModel I have my ValuesDictionary setup as: 在ViewModel中,我的ValuesDictionary设置为:
private Dictionary<ModelEnum, string> _valuesDictionary = new Dictionary<ModelEnum, string>();
public Dictionary<ModelEnum, string> ValuesDictionary
{
get { return _valuesDictionary; }
set { _valuesDictionary = value; OnPropertyChanged(_valuesDictionary); }
}
In my XAML I have: 在我的XAML中,我有:
xmlns:model="clr-namespace:Model.Data;assembly=Model"
...
<TextBox Text="{Binding Path=ValuesDictionary[(model:ModelEnum)ModelEnum.Enum1].Value}" HorizontalAlignment="Left" Height="29" Margin="90,82,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="50"/>
The following XAML snippet: 以下XAML代码段:
(model:ModelEnum)ModelEnum.Enum1
is giving me the error "Parameter type mismatch." 给我错误“参数类型不匹配”。 I'm confused because I thought I was casting this to the Enum type that it was expecting.
我很困惑,因为我认为自己正在将其转换为它期望的Enum类型。 I referenced this SO question to try it with no luck.
我引用了这个 SO问题来试试运气。
replace 更换
(model:ModelEnum)ModelEnum.Enum1].Value
with 与
(model:ModelEnum)Enum1]
Then try. 然后尝试。 I hope it will work.
我希望它能起作用。
Just to add to the potential pitfalls with this, I had issues binding without the explicit "Path=" 只是为了增加潜在的陷阱,我在没有显式“ Path =”的情况下遇到了绑定问题
ie 即
{Binding ValuesDictionary[(model:ModelEnum)Enum1]}
doesn't work, but: 不起作用,但是:
{Binding Path=ValuesDictionary[(model:ModelEnum)Enum1]}
works as expected, although the designer (or maybe ReSharper) still complains about syntax errors. 尽管设计人员(或也许是ReSharper)仍然抱怨语法错误,但仍可以按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.