Advanced combobox binding from code

Suppose I have a business object 'obj1' that has property 'P'.

Let's also assume that I have a list of business objects: 'List<BussObj> list' and each BussObj object contains 2 properties: 'A' and 'B'.

This list is bound to combobox: combobox.ItemsSource = list;.

I would like to specify binding (in C# code) that would bind combobox.SelectedItem.B to my obj.P. How to do it?

I tried something like that but it does not work:

Binding bind= new Binding("B");
bind.Source = obj.P;
comboSubject.SetBinding(ComboBox.SelectedItemProperty, bind);

Thank you in advance for any help

Answers


What exactly doesn't work? Is the corresponding item not selected in the ComboBox, or is P not updated when selection changes? Is the value of P in the list that the ComboBox is bound to?

You might also try verifying that the type of obj implements INotifyPropertyChanged, or is a DependencyObject with P being a DependencyProperty.


Sorry I didnt clarify it. Im using this binding in one way only: after choosing some item in combobox, appriopriate value should be written to obj.P. In that scenario no INotifyPropChanged nor DependencyProperty is necessary


Not sure if you still need this or if this will help, but you can try using the one way to source binding mode as the default would be TwoWay:

Mode = BindingMode.OneWayToSource

Also, I would do it like this, not sure if this helps though:

   comboSubject.SetBinding(
       Selector.SelectedItemProperty,
       new Binding { Source = selectedItemSource, Path = "SelectedItem" });

See if this does the trick...


Need Your Help

Dropdown CSS and HTML Navigation Menu doesn't work in IE6 - IE8

html css internet-explorer nav

I have created a CSS and HTML navigation menu that works in all browsers except IE6 - IE8. How can I make this work? I've attempted to make my css and html as small as possible to help with the

Javascript how to do substring global replacement with specific rule applied

javascript string

I want a function which can replace substring in a big string with specific rule.