C# 以枚举作为键绑定到字典中的值

C# 以枚举作为键绑定到字典中的值,c#,wpf,binding,dictionary,enums,C#,Wpf,Binding,Dictionary,Enums,我是一个应用程序,我想将一些文本框和复选框绑定到字典的值字段(枚举,字符串)。这可能吗?我该怎么做 在xaml代码中,我有类似这样的代码——它使用字符串作为键为Dictionary工作,但无法使用enum正确绑定到键 <dxe:TextEdit EditValue="{Binding Properties[PrimaryAddress], Mode=TwoWay}" /> <dxe:TextEdit EditValue="{Binding Properties[Seconda

我是一个应用程序,我想将一些文本框和复选框绑定到字典的值字段(枚举,字符串)。这可能吗?我该怎么做

在xaml代码中,我有类似这样的代码——它使用字符串作为键为Dictionary工作,但无法使用enum正确绑定到键

<dxe:TextEdit EditValue="{Binding Properties[PrimaryAddress],  Mode=TwoWay}" />
<dxe:TextEdit EditValue="{Binding Properties[SecondaryAddress],  Mode=TwoWay}" />
<dxe:CheckEdit EditValue="{Binding Properties[UsePrimaryAddress], Mode=TwoWay}" />
在ViewModel中,字典定义为:

public Dictionary<MyEnum, string> Properties
公共字典属性
我已经找到了使用枚举值的组合框的解决方案,但这不适用于我的情况


有什么建议吗?

您必须在绑定表达式中为索引器的参数设置适当的类型

视图模型:

public enum Property
{
    PrimaryAddress,
    SecondaryAddress,
    UsePrimaryAddress
}

public class ViewModel
{
    public ViewModel()
    {
        Properties = new Dictionary<Property, object>
        {
            { Property.PrimaryAddress, "123" },
            { Property.SecondaryAddress, "456" },
            { Property.UsePrimaryAddress, true }
        };
    }

    public Dictionary<Property, object> Properties { get; private set; }
}

有关详细信息,请参阅“”

使用上述绑定路径,我收到以下错误:System.Windows.Data错误:40:BindingExpression路径错误:“[]”在“对象“””字典“2”(HashCode=56465364)上找不到属性。BindingExpression:Path=Properties[(mbpt:MyEnum)UsePrimaryAddress];DataItem='MyUserControlViewModel'(HashCode=21018822);目标元素为“CheckEdit”(名称=“”);目标属性是'EditValue'(类型'Object')嗯,没关系。我在绑定路径时犯了一些错误。你的解决方案现在起作用了。谢谢:)太棒了!如果枚举嵌套在类中呢?:)谢谢
public enum Property
{
    PrimaryAddress,
    SecondaryAddress,
    UsePrimaryAddress
}

public class ViewModel
{
    public ViewModel()
    {
        Properties = new Dictionary<Property, object>
        {
            { Property.PrimaryAddress, "123" },
            { Property.SecondaryAddress, "456" },
            { Property.UsePrimaryAddress, true }
        };
    }

    public Dictionary<Property, object> Properties { get; private set; }
}
<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication5"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <TextBox Grid.Row="0" Text="{Binding Path=Properties[(local:Property)PrimaryAddress]}"/>
        <TextBox Grid.Row="1" Text="{Binding Path=Properties[(local:Property)SecondaryAddress]}"/>
        <CheckBox Grid.Row="2" IsChecked="{Binding Path=Properties[(local:Property)UsePrimaryAddress]}"/>
    </Grid>
</Window>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}