Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 更改该DependencyProperty的PropertyChangedCallback中DependencyProperty的值_Wpf_Data Binding_Binding_Dependency Properties_Onchange - Fatal编程技术网

Wpf 更改该DependencyProperty的PropertyChangedCallback中DependencyProperty的值

Wpf 更改该DependencyProperty的PropertyChangedCallback中DependencyProperty的值,wpf,data-binding,binding,dependency-properties,onchange,Wpf,Data Binding,Binding,Dependency Properties,Onchange,我有一个带有组合框的控件: <ComboBox x:Name="TraceComboBox" ItemsSource="{Binding SingleChannelList}" SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type cc:LogicTriggerSimpleL

我有一个带有
组合框的控件

<ComboBox x:Name="TraceComboBox"
          ItemsSource="{Binding SingleChannelList}" 
          SelectedItem="{Binding RelativeSource={RelativeSource  FindAncestor,
                         AncestorType={x:Type cc:LogicTriggerSimpleLevelControl}},
                         Path=SelectedTrace, Mode=TwoWay}">
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];
        }
    }
}
按照我的逻辑,应该发生以下情况:

我在
组合框中选择第三个对象(
SingleChannelList[2]
),就会出现更改处理程序。然后进入
UpdateSelectedTrace()
例程。此时,
SelectedTrace
的值当然是
SingleChannelList[2]
。现在,
UpdateSelectedTrace()
例程强制将
SelectedTrace
属性设置为列表中的第一个对象(
SingleChannelList[0]
),这将触发嵌套在第一个对象中的另一个更改处理程序“SelectedTrace”现在等于SingleChannelList[0],因此组合框还应显示SingleChannelList[0]作为其选择项。

当我跟随调试器直到最后一句粗体字出现时,所有这些都会发生,结果如下:

SelectedTrace
现在等于
SingleChannelList[0]
,但是
组合框将
SingleChannelList[2]
显示为其所选项目。我在
BindingExpression
上尝试了
updateingtarget
,但
SelectedTrace
属性仍然保存值
SingleChannelList[0]
,而
组合框继续显示
SingleChannelList[2]
。这些绑定是安全的,经过测试,在我尝试这么做之前一直有效。有人能告诉我为什么这不能正常工作吗


谢谢

这听起来像是依赖属性“值强制”的场景。值强制将属性的值“推送”到基于所需值的有效值。请在此处阅读更多信息:


我相信这是WPF框架的性能优化。属性更新的原点没有获取propertychanged事件(绑定等价物)来重新更新自身,因为它是更改的原点。您可以使用绑定中的IdentityConverter(仅返回传入值的ValueConverter)强制更新

我发现,考虑到最初的更改源于正在更改的框,这仍然不起作用。相反,我只是在一切都完成后硬设置了这个盒子,它工作得很好。有点老套,但很管用。真正的问题是为什么我最初的场景不起作用?