I am using WPF combo box. I need to know how to do the following: when I select the combobox item in the combo box pop up, it should not update in the content. It should only change when I set SelectedItem
in using Bindings in MVVM.
Please give me any idea.
Set BindingMode OneWay in Binding. BindingMode
您可以将BindingMode用作OneWay来实现相同的目的。
SelectItem="{Binding YourPropertyName Mode=Oneway}"
Selecting item from popup, will set the content. But if you want to stop that as well, you can do that by manually updating Target on selectionChanged event
and make sure binding mode is set to OneWay
so that change doesn't make its way to binded selectedItem in source class.
Sample XAML:
<ComboBox ItemsSource="{Binding Objects}"
SelectionChanged="ComboBox_SelectionChanged"
SelectedItem="{Binding SelectedPropertyInClass, Mode=OneWay}"/>
In code behind:
private void ComboBox_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
var expression = BindingOperations.GetBindingExpression(sender as ComboBox,
ComboBox.SelectedItemProperty);
expression.UpdateTarget();
}
Here, we are getting Binding expression and manually updating the target on any selection change event.
you may use UpdateSourceTrigger=PropertyChanged
<ComboBox Name="cb_users" DisplayMemberPath="Email"
SelectedItem="{Binding selectedUser,Mode= TwoWay,UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding UserList}"> /ComboBox>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.