我在一个可编辑的ComboBox和一个绑定的更新方面遇到了一些问题。目前,我有一个UpdateSourceTrigger=LostFocus的ComboBox,这是因为我需要等待用户完成输入,然后再决定这个值是否是一个新值(从而创建一个新的值)。
不幸的是,我还有另一个特性,当值发生变化时,需要绑定来更新。在这种情况下,LostFocus对我没有好处。当在ComboBox中选择一个新值时,它不会导致LostFocus触发(很明显)。所以我需要找到一种强制更新绑定的方法。
我查看了SelectionChanged并强制对绑定进行更新:
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding ParentConversation.ViewModel.ComboSelectionChanged}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type controls:StoryForgeComboBox}}}"/>
</i:EventTrigger>并在代码后面更新绑定,如下所示:
be = BindingOperations.GetBindingExpression(ele, ComboBox.TextProperty);
if (be != null)
{
be.UpdateSource();
}不幸的是,我无法在此时更新绑定,因为值尚未更改。请参阅此堆栈溢出主题:ComboBox- SelectionChanged event has old value, not new value
有一个技巧,您可以使用DropDownClosed事件,然后更新绑定,但如果使用从不打开ComboBox的向上/向下箭头键,则不起作用。同时,连接到KeyUp和KeyDown还为时过早。装订还不能更新。
所以我的问题是,什么时候是时候说“嘿,Combo Box,你现在可以更新绑定了”。
干杯。
发布于 2014-12-03 16:03:27
可以将SelectionChanged事件触发器更改为LostFocus。
<ComboBox
IsEditable="True"
ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedItem}"
Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Triggers>
<i:EventTrigger
EventName="LostFocus">
<i:InvokeCommandAction
Command="{Binding Command}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>Text将被更新。ComboBox在Items中找到匹配时,SelectedItem将被更改。ComboBox找不到匹配项,并且先前选择了一个项,则SelectedItem设置为null。SelectedItem和Text都会被更新。ComboBox (失去焦点)时,将触发Command。Command。Command也会被触发。这就是你想要的行为?
https://stackoverflow.com/questions/27275376
复制相似问题