Dave
Dave

Reputation: 1659

Setting ComboBox.SelectedIndex Breaks OneWayToSource Binding On ComboBox.SelectedItem

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?

Upvotes: 3

Views: 1323

Answers (1)

Jacob Adams
Jacob Adams

Reputation: 3994

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.

Upvotes: 1

Related Questions