tags:

views:

221

answers:

3

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

A: 

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.

Andy
A: 

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

Martinez
A: 

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...

Richard