tags:

views:

466

answers:

1

Hello All,

The following appears in a data template:

<ComboBox IsSynchronizedWithCurrentItem="False"
          SelectedIndex="0"
          SelectedItem="{Binding Path=Value, Mode=OneWayToSource}"
          ItemsSource="{Binding Path=EnumeratedValues, Mode=OneTime}"/>

With SelectedIndex being set (as shown above), the OneWayToSource binding to Value does not work. If I do not set SelectedIndex the binding to Value is fine.

However, I'd really like to set SelectedIndex to 0 because if I omit setting it, no item is selected by default in the ComboBox.

Can anybody see a way that I can keep my binding to Value working while at the same time ensuring that the first item in the ComboBox is selected by default?

+1  A: 

Why not have a two-way binding and just set the Value property of your object in code behind?

Edit: Added explanation: This allows you to just work with your business object. The point of using data binding is to separate your business objects from the UI and just manipulate your business object and have the UI reflect that.

Jacob Adams
I actually considered this, but for SelectedIndex. Regardless of which I do it for, it's going to be tricky due to the fact that the ComboBox I'm trying to populate Value from is buried down in a data template, and that data template is not always used. I have two data templates (the other has no ComboBox), and the one to use is chosen by a DataTemplateSelector. I do not know how to get at the ComboBox (when it exists) programatically, hence my efforts to work in XAML. But I have a feeling I'm going to have to go in the direction you're suggesting and learn how to do this programatically.
Dave
I am not saying that you should do anything with the data template or the comboboxes. I am saying that you update the business object that your ui is bound to. If you have a two-way binding, the ui will be updated to reflect this change. If you do databinding correctly, you should just be working with business objects, and never doing stuff like cmbCustomer.SelectedIndex=0;
Jacob Adams