/ / Bindung mit Wörterbuchelement nach Schlüssel - wpf, Wörterbuch, Bindung

Bindung mit Dictionary item by key - wpf, Wörterbuch, Bindung

Nehmen wir an, ich habe ein Wörterbuch und möchte Elemente aus diesem Wörterbuch an einige Steuerelemente binden, und ich möchte über den Elementschlüssel binden.

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>

Ist es möglich? Wie kann ich es tun?

Ich möchte ein Wörterbuch (oder etwas Ähnliches) verwenden.

Mein Wörterbuch mit Variablen stammt aus der Datenbank und ich habe etwa 500 Variablen zum Binden.

Diese Variablen sind nicht wie "Liste von Personen" oder so ähnlich. Sie haben eine sehr unterschiedliche Bedeutung und ich möchte sie als "dynamische Eigenschaften" verwenden.

Ich muss jede Variable mit jeder Eigenschaft eines Steuerelements binden.

Antworten:

1 für die Antwort № 1
<ItemsControl x:Name="dictionaryItemsControl" ItemsSource="{Binding dictionary}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Key}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Dafür sollten Sie "Dictionary" als öffentliches Eigentum festlegen und festlegen this.DataContext = this; oder alternativ eingestellt dictionaryItemsControl.ItemsSource = dictionary;


0 für die Antwort № 2

Sie können verwenden

FALL 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-Bindung:

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

FALL # 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 von 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-Bindung:

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