双向数据绑定问题与组合框 - WPF [英] Two way data Binding issue with combo box - WPF
问题描述
我有一个包含 Combobox
的视图。 Combobox
SelectedItem
属性是绑定到 SelectedX
属性视图模型为双向数据绑定。当viewModel初始化时, SelectedX
属性设置正确。但是之后,当视图呈现时,它重置 SelectedX
的值(因为绑定是双向的)。
因此, Combobox
的双向数据绑定基本上不工作。请指教。
这是我的观点的xaml。我首先使用关系和SelectedX的适当值初始化View模型。当视图呈现时,组合框将重置SelectedX的值。 (我想通过添加断点)。希望这有助于
< ComboBox Grid.Row =1Grid.Column =1Margin =5 =WhiteBorderBrush =DarkGray
SelectedItem ={Binding SelectedX,Mode = TwoWay}
ItemsSource ={Binding Relationships}DisplayMemberPath =Value
SelectedValuePath = SelectedValue ={Binding Key,Mode = TwoWay}
IsEditable =FalseIsReadOnly =True/>
解决方案
SelectedValue = {Binding Key,Mode = TwoWay}
这将更改 SelectedItem 到 SelectedValue
。
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" />
解决方案 SelectedValue="{Binding Key, Mode=TwoWay}"
This will change the SelectedItem
to its SelectedValue
.
这篇关于双向数据绑定问题与组合框 - WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!