use*_*108 1 c# wpf enums xaml dictionary
我有几个项目的解决方案.在一个项目中,我的模型是一个名为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); }
}
Run Code Online (Sandbox Code Playgroud)
在我的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"/>
Run Code Online (Sandbox Code Playgroud)
以下XAML代码段:
(model:ModelEnum)ModelEnum.Enum1
Run Code Online (Sandbox Code Playgroud)
给我错误"参数类型不匹配".我很困惑,因为我以为我把它投射到它所期望的Enum类型.我引用了这个问题来试试它没有运气.
更换
(model:ModelEnum)ModelEnum.Enum1].Value
Run Code Online (Sandbox Code Playgroud)
同
(model:ModelEnum)Enum1]
Run Code Online (Sandbox Code Playgroud)
然后试试.我希望它能奏效.
小智 5
只是为了增加潜在的陷阱,我在没有明确的“Path=”的情况下绑定了问题
IE
{Binding ValuesDictionary[(model:ModelEnum)Enum1]}
Run Code Online (Sandbox Code Playgroud)
不起作用,但是:
{Binding Path=ValuesDictionary[(model:ModelEnum)Enum1]}
Run Code Online (Sandbox Code Playgroud)
按预期工作,尽管设计者(或者可能是 ReSharper)仍然抱怨语法错误。