我有几个项目的解决方案。在一个项目中,我的模型是一个名为ModelEnum的枚举。
然后在我的WPF项目中,我有一个ViewModel,它有一个Dictionary。
在我的ViewModel中,我将ValuesDictionary设置为:
private Dictionary<ModelEnum, string> _valuesDictionary = new Dictionary<ModelEnum, string>();
public Dictionary<ModelEnum, string> ValuesDictionary
{
get { return _valuesDictionary; }
set { _valuesDictionary = value; OnPropertyChanged(_valuesDictionary); }
}
在我的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"/>
以下XAML代码段:
(model:ModelEnum)ModelEnum.Enum1
给我错误&#34;参数类型不匹配。&#34;我很困惑,因为我以为我把它投射到了它期待的Enum类型。我引用了this SO问题来试试它没有运气。
答案 0 :(得分:5)
替换
(model:ModelEnum)ModelEnum.Enum1].Value
与
(model:ModelEnum)Enum1]
然后试试。我希望它能奏效。
答案 1 :(得分:2)
只是为了增加潜在的陷阱,我没有明确的#34; Path =&#34;
即。
{Binding ValuesDictionary[(model:ModelEnum)Enum1]}
不起作用,但是:
{Binding Path=ValuesDictionary[(model:ModelEnum)Enum1]}
按预期工作,尽管设计师(或者ReSharper)仍然抱怨语法错误。