gpt4 book ai didi

wpf - 如何在父控件中绑定(bind)子用户控件的数据上下文

转载 作者:行者123 更新时间:2023-12-04 15:43:23 24 4
gpt4 key购买 nike

<TextBlock Text="{Binding ChildGroupName, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True,Mode=TwoWay}"
TargetUpdated="OnTextUpdated"/>

这里 ChildGroupName是子控件数据上下文属性。我要绑定(bind) ChildGroupName父窗口的值。

最佳答案

您不能使用 FindAncestor将数据绑定(bind)到后代的数据......线索就在它的名字中。如果 child UserControl在与父级相同的 XAML 中定义,然后您可以为其提供名称,然后使用 Binding.ElementName Property将数据绑定(bind)到其属性:

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

关于wpf - 如何在父控件中绑定(bind)子用户控件的数据上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29011218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com