Binding with dictionary item by key

邮差的信 提交于 2021-02-09 09:28:11

问题


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:

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

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.


回答1:


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




回答2:


You can use

CASE #1

C# Code:

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:

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

CASE #2

C# Code: 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

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:

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


来源:https://stackoverflow.com/questions/38264180/binding-with-dictionary-item-by-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!