使用枚举键将WPF数据绑定到字典 [英] WPF data binding to dictionary with enum key

查看:389
本文介绍了使用枚举键将WPF数据绑定到字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个项目的解决方案.在一个项目中,我的模型有一个名为ModelEnum的枚举.

I have a solution with a couple of projects. In one project, is my model that has an enum called ModelEnum.

然后在我的WPF项目中,我有一个带有字典的ViewModel.

Then in my WPF project I have a ViewModel which has a Dictionary.

在ViewModel中,我的ValuesDictionary设置为:

And in my ViewModel I have my ValuesDictionary setup as:

    private Dictionary<ModelEnum, string> _valuesDictionary = new Dictionary<ModelEnum, string>();

    public Dictionary<ModelEnum, string> ValuesDictionary
    {
        get { return _valuesDictionary; }
        set { _valuesDictionary = value; OnPropertyChanged(_valuesDictionary); }
    }

在我的XAML中,我有:

In my XAML I have:

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

给我错误参数类型不匹配".我很困惑,因为我认为我正在将其转换为它期望的Enum类型.我引用了这个 SO问题来尝试没有运气.

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. I referenced this SO question to try it with no luck.

推荐答案

替换

(model:ModelEnum)ModelEnum.Enum1].Value

使用

(model:ModelEnum)Enum1]

然后尝试.我希望它能起作用.

Then try. I hope it will work.

这篇关于使用枚举键将WPF数据绑定到字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆