且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在父级中绑定子用户控件的数据上下文

更新时间:2023-10-11 10:30:16

您不能使用 FindAncestor 将数据绑定到后代的数据...线索就在其名称中.如果子 UserControl 在与父项相同的 XAML 中定义,则您可以为其提供名称,然后使用 Binding.ElementName 属性 将数据绑定到其属性:

You cannot use FindAncestor to data bind to a descendant's data... the clue is in its name. If the child UserControl is defined in the same XAML as the parent, then you can provide it with a name and then use the Binding.ElementName Property to data bind to its properties:

<TextBlock Text="{Binding ChildPropertyName, ElementName=NameOfChildUserControl, 
    UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, Mode=TwoWay}" 
    TargetUpdated="OnTextUpdated" />