我有一个带有 ComboBox
的控件:
<ComboBox x:Name="TraceComboBox"
ItemsSource="{Binding SingleChannelList}"
SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type cc:LogicTriggerSimpleLevelControl}},
Path=SelectedTrace, Mode=TwoWay}">
这是在包含 ComboBox 的 OuterControl 中,用于属性 SelectedTrace 的 PropertyChangedCallback:
PropertyChangedCallback
private static void OnSelectedTraceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
OuterControl oc = d as OuterControl ;
oc.UpdateSelectedTrace();
}
private void UpdateSelectedTrace()
{
ViewModelType vm = DataContext as ViewModelType;
if (vm != null)
{
if (vm.SingleChannelList != null)
{
SelectedTrace = vm.SingleChannelList[0];
}
}
}
根据我的逻辑,以下情况应该发生:
我选择
ComboBox
中的第三个对象(SingleChannelList[2]
),然后会触发更改处理程序。接下来,它进入UpdateSelectedTrace()
例程。此时,SelectedTrace
的值当然是SingleChannelList[2]
。现在,UpdateSelectedTrace()
例程强制将SelectedTrace
属性设置为列表中的第一个对象(SingleChannelList[0]
),这会触发嵌套在第一个处理程序内部的另一个更改处理程序。“SelectedTrace”现在等于SingleChannelList[0],因此ComboBox也应显示SingleChannelList[0]作为其选择。
当我一直跟着调试器走到粗体字的最后一句话时,所有这些都发生了,但实际上却像这样:
SelectedTrace
现在等于SingleChannelList[0]
,但ComboBox
显示SingleChannelList[2]
作为其选定项目。 我尝试在BindingExpression
上进行UpdatingTarget
,但是SelectedTrace
属性仍然保持值SingleChannelList[0]
,而ComboBox
继续显示SingleChannelList[2]
。这些绑定是安全的、经过测试的,并且一直工作得很好,直到我尝试做这件事情。有人能告诉我为什么这不起作用吗?谢谢