wpf 通过键与字典项绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38264180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:52:21  来源:igfitidea点击:

Binding with dictionary item by key

wpfdictionarybinding

提问by Kamil

Let's say I have some dictionary and I want to bind items from this dictionary to some controls and I want to bind by item key.

假设我有一些字典,我想将这个字典中的项目绑定到一些控件,我想按项目键绑定。

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        Dictionary<string,string> dictionary = new Dictionary<string,bool>();

        dictionary.Add("Item 1", "one");
        dictionary.Add("Item 2", "two");
        dictionary.Add("Item 3", "three");
        // ...
        dictionary.Add("Item 99", "ninety nine");

        // ...

        this.DataContext = //...
    }
}

XAML:

XAML:

<Window ... >
    <Grid>
        <Label Content="{Binding ...}"/><!-- "Item 1" -->
        <TextBox Text="{Binding ...}"/><!-- "Item 3" -->

        <StackPanel>
             <CustomControl CustomProperty="{Binding ...}"/><!-- "Item 77" -->
             <CustomControl2 CustomProperty="{Binding ...}"/><!-- "Item 99" -->
        </StackPanel>
    </Window>

Is it possible? How can I do it?

是否可以?我该怎么做?

I want to use dictionary (or something sililar).

我想使用字典(或类似的东西)。

My dictionary with variables comes from database and I have about 500 variables to bind.

我的变量字典来自数据库,我有大约 500 个变量要绑定。

These variables are not like "list of persons" or something like that. They have very diffrent meaning and I want use them as "dynamic properties".

这些变量不像“人员列表”或类似的东西。它们具有非常不同的含义,我想将它们用作“动态属性”。

I need to bind any variable with any property of any control.

我需要将任何变量与任何控件的任何属性绑定。

回答by cdmnk

<ItemsControl x:Name="dictionaryItemsControl" ItemsSource="{Binding dictionary}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Key}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

For that you should make 'dictionary' public property, and set this.DataContext = this;or alternatively set dictionaryItemsControl.ItemsSource = dictionary;

为此,您应该将“字典”设为公共财产,并设置this.DataContext = this;或替代设置dictionaryItemsControl.ItemsSource = dictionary;

回答by B.Balamanigandan

You can use

您可以使用

CASE #1

情况1

C# Code:

C# 代码:

public partial class MainWindow : Window
{
    Dictionary<string, object> _data = new Dictionary<string, object>();

    public MainWindow()
    {
        InitializeComponent();

        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {

        _data["field1"] = 100;
        _data["field2"] = "Pete Brown";
        _data["field3"] = new DateTime(2010, 03, 08);

        this.DataContext = new DicVM();

    }
}

XAML Binding:

XAML 绑定:

<TextBlock Text="{Binding [field1], Mode=TwoWay}" />

CASE #2

案例#2

C# Code: DicViewModel.cs

C# 代码: DicViewModel.cs

class DicViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private Dictionary<string, object> dict = new Dictionary<string, object>();

    public Dictionary<string, object> Dict
    {
        get { return dict; }
        set
        {
            dict = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Dict"));
        }
    }

    public DicVM()
    {
        Dict["field1"] = 100;
        Dict["field2"] = "Pete Brown";
        Dict["field3"] = new DateTime(2010, 03, 08);
    }

}

C# Code of Dic.xaml.cs

C# 代码 Dic.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = new DicViewModel();
    }
}

XAML Binding:

XAML 绑定:

<TextBlock Text="{Binding Dict[field1], Mode=TwoWay}" />