Binding to a dictionary - wpf

I think I have a rather interesting problem. I have a model where the main data structure really needs to be a dictionary.
I want to bind the values of that dictionary to a listbox.
I have an ObservableDictionary class that fires collection changed events when necessary.
Unfortunately if I bind to this observable dictionary, the listbox is populated with KeyValuePairs and I can't make an IValueConverter for it because KeyValuePairs aren't objects, they're structs.
Is there some way in xaml to specify that I want the .value of all the Pairs?
If I just return the values of the Dictionary in the property I'm binding to, that list isn't observable and so doesn't update with the dictionary.
Is there a way to use INotifyPropertyChanged to force the UI to update when binding to a collection?
Thanks!

Use DisplayMemberPath or ItemTemplate.

Related

ObservableCollection<T> exposed as IEnumerable<T> still treated as former?

I have a view model that exposes a ObservableCollection<TInheritor> collection as IEnumerable<TBaseInterface> (for the covariance). If I bind that property to the ItemsSource property on a ListBox, is that ListBox still smart enough to use my source as an ObservableCollection?
Yes it will do that, there shouldn't be any problems using an ObservableCollection like this. Here is an example: http://www.c-sharpcorner.com/UploadFile/e06010/observablecollection-in-wpf/

Updating an Observable Collection Based on a combobox selection

So I have an ObservableCollection of items called "Class1" and
Class1 has a property named "ID".
I use a datagrid from the WPFToolkit and bind to this collection.
Within the datagrid is a combobox column and I bind it's ItemsSource to the ID property of the class.
At this point, all is good and everything populates as it should. What I want to do is modify the ObservableCollection to reflect the value selected in the ComboBox.
I have a ValueConverter bound to the SelectedItemBinding on the ComboBox as follows:
SelectedItemBinding="{Binding Path=ID, Converter={StaticResource IDConverter}}
What is the best (i.e: WPF approved method) of modifying the collection? When the IDConverter ConvertBack() method is called, I get the appropriate Class1 instance, but I can't access the Observable collection from within the ValueConverter and I also don't have access to the SelectedIndex value from the Datagrid.
I could create a class as a static resource with a pointer to my collection and pass that as a ConverterParameter, but that seems sort of hokey and I'm assuming there must be some slicker way of doing this with databinding.
For the record, a simple solution is to create a local resource with a reference to the collection you wish to modify as a dependency property. You can then pass this as a ConverterParameter and have access to it in the ConvertBack() interface method.
A caveat: You will most likely encounter a DeferRefresh exception when you make changes to the collection and then you lose focus.
An excellent fix is found here:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/187b2b8f-d403-4bf3-97ad-7f93b4385cdf

ItemsControl - Bind Control's and Backing Collection's Sort Orders Together

Is there a way to bind an ItemsControl (like ListView or DataGrid) to a sorted collection such that:
whenever the collection is resorted via code, the ItemsControl automatically reflects the new sort order,
AND whenever the ItemsControl is sorted, it re-sorts the collection?
Thank you,
Ben
You'll need to use the
CollectionViewSource.GetDefaultView()
method to get the default view of your ObservableCollection and apply the sort on that.
For example, below I'm sorting the ObservableCollection named 'authors' by BookTitle.
ObservableCollection<Author> authors = new ObservableCollection<Author>();
authors = PopulateCollection();
// Sort by BookTitle
System.ComponentModel.ICollectionView colView;
colView = CollectionViewSource.GetDefaultView(authors);
colView.SortDescriptions.Add(new System.ComponentModel.SortDescription("BookTitle", ListSortDirection.Descending));
try to define this two attributes on the bining:
IsSynchronizedWithCurrentItem=true
BindsDirectlyToSource=true
i did not tried this but it might work..
Putting your items into an ObservableCollection and then binding to the ObservableCollection should do the trick. Any sorting done on the ObservableCollection should "translate" to the UI layer.

(WPF) Binding two classes to a control

I have two viewmodels. One which displays a collection of IPAddresses, and one which displays a collection of objects that has numerous parameters. One of these parameters is an IPAddress. So, I have another panel that binds to the properties of the second object. I would like a combobox to have the ItemSource set to the first object, but the selected item bound to the second object. However, I can only seem to set one datacontext on a control in code behind. Is there any way around this? I would prefer to do this all in code behind if possible (i find the xaml programming to be non-ideal at best), but I'll take anything.
For the ComboBox bind the collection of IPAddresses to the ItemsSource property, and bind the SelectedItem of the ComboBox to the IPAddress property of the SelectedItem of the collection of "numerous property objects".
This would be easier to answer if I had a better description of your objects including names. But it sounds to me like you should make a dictionary with the ipaddress as the key and the second object as the value.
If you can do that then you can bind to it in code like so:
comboBox.ItemsSource = dictionary;
comboBox.DisplayMemberPath = "Key";
comboBox.SelectedValuePath = "Value";
This is assuming that you have exactly one "second object" for every IPAddress in the collection. Which sounds about right given your description.
Take a look at the Source property of Bindings. It is basically a DataContext for a specific binding. It should make what you're trying to do very simple, especially in code behind.

autoupdate a list in wpf

I want to display a bunch of Objects i have created in a ListBox. My objects implement the INotifyPropertyChanged Interface.
I tried to use an ObservableCollection, which i have bound to a listbox Control (listbox1.DataContext = MyCollection)
But this does not exactly what i want to do, because the Listbox is not refreshed when one of the properties of one of my objects in MyCollection changes.
I have found this blogposting: http://sweux.com/blogs/psampaio/index.php/2009/04/13/creating-a-custom-observable-collection-in-wpf
is this realy the easyiest/only way to keep track of several objects?
I'm not sure, but have you tried using a datatemplate for your listbox items? like, a textbox that explicitly sets it's text to the appropriate binding.

Resources