Updating an Observable Collection Based on a combobox selection - wpf

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

Related

Binding to a dictionary

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.

Updating SelectedItem in DataGrid ByRef and Retaining SelectedItem Behavior WPF

I have a datagrid with an Observable Collection of a custom object. The selectedItem binding is set to SelectedCustObject in my view model. I've wired up a property Changed handler so I can update the database anytime the user makes a change to the selectedItem. When the property is updated my Save() sub fires and passes the Custom Object ByRef to my middle layer (WCF) and returns it. The datagrid loses it's SelectedItem state at this point. I'm not sure if I'm going about this right or if there is a better way.
SelectedItem="{Binding Path=SelectedMeterUsage, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
It sounds as if the reference SelectedMeterUsage is changing, and no longer pointing to the same instance that is in your ObservableCollection, therefore the grid no longer knows which item is selected. You will either need to change your update code to not change the reference, or after the save, you would need to reset the reference by finding the item in the ObservableCollection with the same ID and assigning it to SelectedMeterUsage.

Wpf treeview selectedItem databinding

I have a view where I've got an object bound to a treeview. The object has a number of collections (of different types) so I'm using hiearchical templates with a CompositeCollection to display them in the treeview.
I've then got a textbox that is bound to the treeview's selectedItem. Here I'm serialising the selectedItem to XML and displaying it in the textbox for editing.
All good so far. However, the big problem I have is that I can't use 2-way databinding with the SelectedItem property of the treeview as it is read only.
How can I cleanly keep the textbox edits in sync with my object that is bound to the treeview?
I do not think you need to do two-way databinding on the SelectedItem itself, you should expose a property in the class of your bound object which returns the serialized string and upon set modifies the object appropriately. This should be easier than dealing with the object as a whole.
Your XML stream must be represented as a property on your SelectedItem node, and your TextBox must be bound to that, somehow. The SelectedItem is read-only, but the object it refers to is not. If you two-way bind on that property, you should be able to affect your edits correctly. This would be done in the DataTemplates and HiearchicalDataTemplates you are using, since they are bound to the underlying data representation of the nodes you are representing with the TreeView.

Silverlight: Two-way Binding for a DependencyProperty (IList) not working

Well..its working BUT only when user creates a property that is of type List.
Here's the complete scenario.
I have a templated control (Multi Select ComboBox with Checkboxes)
User gives a List/ObservableCollection as its ItemsSource. The collection can be of any type (Employee, Chair, Person etc). The DependencyProperty for ItemsSource is of Type IList.
User can also give a List with two-way binding in a DependencyProperty called SelectedItems (so that he can show some items as checked and get back the items which gets checked)
Now the issue is with the SelectedItems dp. It is of type IList in the templated control.
The two-way binding does not work if user has bind it to, say, List<Person>.
But it works if List<Person> is changed to List<object>.
I cant figure out what is it that I am not doing right!
It doesn't make sense to place a TwoWay binding on the SelectedItems property. This would imply that the control should create and assign an instance of an object implementing IList to the property on the source object. However there is no way for control to know what actual type to create to assign to the property.
Instead you should be using a OneWay bind to a List that pre-exists in the source object albeit an empty one. The controls task then is to simply add or remove members in that list from the list supplied in the ItemsSource property.

WPF - Combobox SelectedItem not getting set?

I have a ComboBox that has its ItemsSource bound to a static List<CustomSettings> of options. The ComboBox is part of a form which is bound to a CustomObject class, and one of the properties on that class is a CustomSettingProperty.
I would like to bind the SelectedItem of the ComboBox to the property specified in the CustomObject, however SelectedItem="{Binding Path=CustomSettingProperty}" is not setting the default selected item. Using breakpoints I can see that it is calling the get; method, so I think the problem might be in the fact the CustomSettingProperty is created separately from the List<CustomObject> so WPF does not think it is the same item.
Is there an easy way to do this? Or perhaps an alternative since the CustomSettings class does contain an Id?
If the item that is selected is not the same instance that is contained in the List, you must override Equals() in the CustomObject to let the ComboBox know that it is the same object.
If it's the same instance, maybe it's only a simple thing such as setting the BindingMode to TwoWay:
SelectedItem="{Binding Path=CustomSettingProperty,Mode=TwoWay}"
I found the solution, It was The Prism's Event Aggregator was passed with reference type so That the ui thread stops processing

Resources