gpt4 book ai didi

wpf - 当父类型可以不同时,如何访问 wpf 中父的 DataContext?

转载 作者:行者123 更新时间:2023-12-02 01:14:06 25 4
gpt4 key购买 nike

我需要在 wpf xaml 中访问父级的 DataContext。整个 xaml 页面代码是动态的。所以不知道 parent 的类型。

我在写这个

<Grid DataContext={Binding Path=.}>

这是正确的吗?

最佳答案

请记住,如果未显式设置 DataContext,它将继承其父级的 DataContext。如果出于某种原因,这不起作用,您应该查看与 RelativeSource 的绑定(bind).

这样的事情可能会起作用:

<Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext}}"

假设 Grid 有一个 Window 类型的祖先(我认为所有控件都应该有)。

关于wpf - 当父类型可以不同时,如何访问 wpf 中父的 DataContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13582753/

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