Kamil
Kamil

Reputation: 13941

Binding with dictionary item by key

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.

Upvotes: 4

Views: 10414

Answers (2)

B.Balamanigandan
B.Balamanigandan

Reputation: 4875

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}" />

Upvotes: 1

cdmnk
cdmnk

Reputation: 314

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

Upvotes: 3

Related Questions