wpf - 更改该 DependencyProperty 的 PropertyChangedCallback 中的 DependencyProperty 的值

标签 wpf data-binding 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}">

这是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]作为其选定的项目。我试过UpdatingTargetBindingExpression仍然是 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/

相关文章:

silverlight - 在 Silverlight 中,如何根据在列表框中选择的项目设置启用按钮的启用状态?

c# - 自动完成导致文本向右滚动

c# - WPF 自定义控件默认数据绑定(bind)

c# - FlowDocument 强制分页 (BreakPageBefore)

c# - WPF - 恢复 ComboBox ItemSsource 中的上一个 SelectedItem 已更改

c# - 如何在 WPF 中将集合绑定(bind)到 ListView

WPF 绑定(bind) : Casting in binding path

wpf - 'System.Windows.Media.Animation.DoubleAnimation' 类型的 AnimationTimeline 不能用于为 'Row' 类型的 'System.Int32' 属性设置动画

javascript - Google Chrome 无法打开多个邮件客户端

r - lapply/R中的 promise