Two way data Binding issue with combo box - WPF

两盒软妹~` 提交于 2019-12-23 02:58:07

问题


I have a View that contains a Combobox. The Combobox SelectedItem property is data bound to SelectedX property of View Model as two way data binding. When the viewModel is initialized, the SelectedX property is set correctly. But after that when the view renders, it resets the value of SelectedX(since the binding is two-way). So the two way data binding for the Combobox is basically not working. Please advise.

This is the xaml for my view. I initialize the View model first with apprpriate values for Relationships and SelectedX. When the view renders, the combo box resets the value for SelectedX. (I figured that by adding breakpoints). Hope this helps

   <ComboBox Grid.Row="1" Grid.Column="1" Margin="5" Background="White"     BorderBrush="DarkGray"
              SelectedItem="{Binding SelectedX, Mode=TwoWay}" 
              ItemsSource="{Binding Relationships}" DisplayMemberPath="Value" 
              SelectedValuePath="Value" SelectedValue="{Binding Key, Mode=TwoWay}"
              IsEditable="False" IsReadOnly="True" />

回答1:


SelectedValue="{Binding Key, Mode=TwoWay}"

This will change the SelectedItem to its SelectedValue.



来源:https://stackoverflow.com/questions/21227498/two-way-data-binding-issue-with-combo-box-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!