我有一个带有 ComboBox
的控件:
<ComboBox x:Name="TraceComboBox"
ItemsSource="{Binding SingleChannelList}"
SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type cc:LogicTriggerSimpleLevelControl}},
Path=SelectedTrace, Mode=TwoWay}">
这是
PropertyChangedCallback
为属性(property) SelectedTrace
在包含 ComboBox
的 OuterControl 中: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]
作为其选定的项目。我试过UpdatingTarget
在 BindingExpression
仍然是 SelectedTrace
属性(property)持有值(value)SingleChannelList[0]
而 ComboBox
继续显示SingleChannelList[2]
.这些绑定(bind)是安全且经过测试的,并且在我尝试这样做之前一直有效。谁能告诉我为什么这不能正常工作?谢谢
最佳答案
这听起来像是依赖属性“值强制”的场景。值强制将属性的值“推”到基于期望值的有效值。在此处阅读更多信息:
Dependency Property Callbacks and Validation
关于wpf - 更改该 DependencyProperty 的 PropertyChangedCallback 中的 DependencyProperty 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2057894/