Binding C# WPF controls to a dictionary?

Instead of using a Dictionary as your DataContext I'd create a custom object like MainViewModel Give it properties that correspond to item1, item2, etc, except give them appropriate names. Then use TextBox Text="{Binding MyPropertyName} To handle updates, you can either set your DataContext to a new MainViewModel object or you can set up your class to broadcast property changes. You can do that either through INotifyPropertyChanged on the class or with dependency properties At least that's what it seems like you're trying to accomplish.

If you're going for displaying an arbitrary number of controls you'd need something different.

Instead of using a Dictionary as your DataContext I'd create a custom object like MainViewModel. Give it properties that correspond to item1, item2, etc, except give them appropriate names. Then use .

To handle updates, you can either set your DataContext to a new MainViewModel object or you can set up your class to broadcast property changes. You can do that either through INotifyPropertyChanged on the class or with dependency properties. At least that's what it seems like you're trying to accomplish.

If you're going for displaying an arbitrary number of controls you'd need something different.

Based on the question I don't think this user is ready to tackle the MVVM pattern...just sayin – tsells Sep 29 at 3:54 Lol, it's true. I'm a beginner, so I should make some compromises to keep it simple. What I liked about dictionary is my index becomes a string key - not an arbitrary number.

I think I’ll have 100 named items bound to various controls (sliders, editBoxes, radioButtons)…the list is fixed (more or less) but it is long. Making each item a property is likely a good approach for me, but it will be a lot to type ;) Thanks for answering. – Danny Maher Sep 30 at 1:30.

Here is an example of how I would do it. Main Class (Code Behind) /// /// Interaction logic for MainWindow. Xaml /// public partial class MainWindow : Window, INotifyPropertyChanged { private List _myObjects; public List MyObjects { get { return _myObjects; } set { _myObjects = value; if(PropertyChanged!

= null) { PropertyChanged(this, new PropertyChangedEventArgs("MyObjects")); } } } public MainWindow() { MyObjects = new List(); // Add 20 records for sample data for (int I = 0; i.

Note that if you want auto UI updates for adding / removing items from the collection change your list to an ObservableCollection. – tsells Sep 29 at 4:15 Thanks for the detailed answer. Each UI control would bind to a specific MyObject instance.

I’m guessing each MyObject in the list is indexed by a number - rather than a dictionary key string. Using a number instead of a name would be a reasonable compromise. There are quite a variety of controls so I’ll need to manually index each object.

And thanks for showing an example of how PropertyChanged should work...that's a big help for me. – Danny Maher Sep 30 at 2:51.

A dictionary is definitly not a convenient way to do a two way data binding in WPF. It seems an ObservableCollection is more suited to your requirements. Something like: public class ItemsList : ObservableCollection { public ItemsList() : base() { Add(new Item("item 1", 100)); Add(new Item("item 2", 120)); Add(new Item("item 3", 140)); Add(new Item("item 4", 160)); } } Item is a simple class with a name and a value properties.

I have ommitted it here because it is self explanatory. The advantage here is that you can bind to a dynamic number of items not only the ones declared imperatively. Once you bind you datacontext to it, you get the automatic property notification for two way databinding.

Your XAML will have to change to accomodate binding to a collection of course. Maybe an ItemsControl that takes that collection as its ItemsSource.

That won't work if he wants some to be TextBoxes and some to be Sliders. – RandomEngy Sep 29 at 15:24.

I've tried tsell's example, using his class in a list. The list is just a convenient way to generate and manage a fixed number of elements. The Item1 WPF control binds to the Item1 object and its value.

The object is found by its index number. The binding and dataContext in this case is simple enough for me to use (as a beginner). It works, but I'm not sure it's exactly an elegant way to do it.

Public MainWindow() { MyObjects = new List(); MyObject item1 = new MyObject(); item1. MyValue = string. Format("100"); MyObjects.

Add(item1); MyObject item2 = new MyObject(); item2. MyValue = string. Format("120"); MyObjects.

Add(item2); MyObject item3 = new MyObject(); item3. MyValue = string. Format("140"); MyObjects.

Add(item3); MyObject item4 = new MyObject(); item4. MyValue = string. Format("160"); MyObjects.

Add(item4); InitializeComponent(); DataContext = this; } xaml BTW, I will change to int for MyValues..they are all int numbers. For now it is a string.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions