为该DependencyProperty更改PropertyChangedCallback内的DependencyProperty的值

Kam*_*eko 5 data-binding wpf binding dependency-properties onchange

我有一个控件ComboBox:

<ComboBox x:Name="TraceComboBox"
          ItemsSource="{Binding SingleChannelList}" 
          SelectedItem="{Binding RelativeSource={RelativeSource  FindAncestor,
                         AncestorType={x:Type cc:LogicTriggerSimpleLevelControl}},
                         Path=SelectedTrace, Mode=TwoWay}">
Run Code Online (Sandbox Code Playgroud)

这是OuterControl中包含以下PropertyChangedCallback属性SelectedTraceComboBox:

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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

根据我的逻辑,应该发生以下情况:

我在ComboBox(SingleChannelList[2])中选择第3个对象,并发生更改处理程序.然后它进入UpdateSelectedTrace()例行程序.此时,价值SelectedTrace当然是SingleChannelList[2].现在,UpdateSelectedTrace()例程强制将SelectedTrace属性设置为list(SingleChannelList[0])中的第一个对象,该对象触发嵌套在第一个对象中的另一个更改处理程序. "SelectedTrace"现在等于SingleChannelList [0],因此ComboBox也应该显示SingleChannelList [0]作为其选择.

所有这一切都发生在我跟随调试器,直到粗体的最后一句话,而不是像这样:

SelectedTrace现在等于SingleChannelList[0],但ComboBox显示SingleChannelList[2]为其选定项目.我试过UpdatingTargetBindingExpression,仍然,SelectedTrace财产保持价值,SingleChannelList[0]ComboBox继续显示SingleChannelList[2].这些绑定是安全的并经过测试,并且一直有效,直到我尝试这样做.任何人都可以告诉我为什么这不能正常工作?

谢谢

Avi*_* P. 2

这听起来像是依赖属性“值强制”的场景。值强制将属性的值“推”为基于所需值的有效值。在这里阅读更多相关信息:

依赖属性回调和验证