绑定到具有枚举作为关键字的Dictionary中的值 [英] binding to value in Dictionary with enum as a key

查看:247
本文介绍了绑定到具有枚举作为关键字的Dictionary中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在xaml代码中,我有这样的东西 - 它使用字符串作为键,但它无法正确绑定使用枚举键入

 < dxe:TextEdit EditValue ={Binding Properties [PrimaryAddress],Mode = TwoWay}/> ; 
< dxe:TextEdit EditValue ={Binding Properties [SecondaryAddress],Mode = TwoWay}/>
< dxe:CheckEdit EditValue ={Binding Properties [UsePrimaryAddress],Mode = TwoWay}/>

..这里是我在枚举中所拥有的

  public enum MyEnum 
{
PrimaryAddress,
SecondaryAddress,
UsePrimaryAddress
}

在ViewModel字典中定义为:

  public Dictionary< MyEnum,string>属性

我已经找到具有枚举值的组合框的解决方案,但这不适用于我的情况。 p>

任何建议?

解决方案

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



查看模型:

  public enum Property 
{
PrimaryAddress,
SecondaryAddress,
UsePrimaryAddress
}

public class ViewModel
{
public ViewModel()
{
属性=新字典<属性,对象>
{
{Property.PrimaryAddress,123},
{Property.SecondaryAddress,456},
{Property.UsePrimaryAddress,true}
};
}

public Dictionary< Property,object>属性{get;私人集合}
}

XAML:

 < 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 =MainWindowHeight = 350宽度=525>
< Grid>
< Grid.RowDefinitions>
< RowDefinition />
< RowDefinition />
< RowDefinition />
< /Grid.RowDefinitions>

< TextBox Grid.Row =0Text ={Binding Path = Properties [(local:Property)PrimaryAddress]}/>
< TextBox Grid.Row =1Text ={Binding Path = Properties [(local:Property)SecondaryAddress]}/>
< CheckBox Grid.Row =2IsChecked ={Binding Path = Properties [(local:Property)UsePrimaryAddress]}/>
< / Grid>
< / Window>

代码隐藏:

  public partial class MainWindow:Window 
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}

有关详细信息,请参阅绑定路径语法


I'm some application and i would like to bind some textboxes and chekcboxes to value field of Dictionary(Enum, string). Is this possible and how can I do that?

In xaml code I have something like this - it is working for Dictionary with string as a key, but it cannot correctly bind to key with 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}" />

.. and here is what I have in Enum

public enum MyEnum
{
    PrimaryAddress,
    SecondaryAddress,
    UsePrimaryAddress
}

In ViewModel dictionary is defined as:

public Dictionary<MyEnum, string> Properties

I have found solution for combobox with enum values but this does not apply to my case.

Any advice?

解决方案

You have to set appropriate type for indexer's parameter in binding expression.

View model:

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; }
}

XAML:

<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>

Code-behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}

For more info, see "Binding Path Syntax".

这篇关于绑定到具有枚举作为关键字的Dictionary中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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