简体   繁体   English

设置ComboBox.SelectedIndex会破坏ComboBox.SelectedItem上的OneWayToSource绑定

[英]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. 设置SelectedIndex (如上所示),与Value绑定的OneWayToSource将不起作用。 If I do not set SelectedIndex the binding to Value is fine. 如果我不设置SelectedIndex绑定到Value很好。

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 . 但是,我真的很想将SelectedIndex设置为0,因为如果忽略设置它,则默认情况下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? 有人可以看到一种方法,让我保持与Value绑定,同时确保默认情况下选中ComboBox中的第一项吗?

Why not have a two-way binding and just set the Value property of your object in code behind? 为什么不进行双向绑定,而仅在后面的代码中设置对象的Value属性?

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. 使用数据绑定的目的是将您的业务对象与UI分离开来,而只需操作您的业务对象并使UI反映出来即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM