从WPF XAML视图中的一个控件中,我需要访问另一个控件的属性,只有在上到一个公共父控件然后从该父控件向下移动时,我才能在可视树中访问它。
例如:
<PageUserControl>
<Grid>
<TextBlock Text="Some example text" />
</Grid>
<TextBlock Text="{Binding Source={RelativeSource Mode=FindAncestor, AncestorType=PageUserControl, Path=??? I want to access the TextBlock}" />
</PageUserControl>
我想从第二个文本块访问第一个文本块的text属性(这只是一个示例)。
我需要的是一种组合相对源的方法,第一个方法是向上浏览可视树并找到PageUserControl,第二个方法是从此处向下浏览可视树并找到网格,最后是第三个网格中的文本块。
我在这里错过了什么吗?还是不可能?
我无法添加控件ID或类似的内容,它只能用于控件类型。
我在考虑采用XPath语法的相对源之类的东西,但这似乎是为了其他目的(绑定XML文档)。
也许是另一个主意?
谢谢!
答案 0 :(得分:1)
我找到了解决问题的方法。可以使用这种方法:
<PageUserControl>
<Grid>
<TextBlock Text="Some example text" />
</Grid>
<TextBlock Text="{Binding Path=Children[0].Children[0].Text,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=PageUserControl}}" />
</PageUserControl>
虽然不是很灵活,但对我来说已经足够了。