[英]How do I determine AncestorLevel for RelativeSource in WPF?
In a WPF application using a fairly standard MVVM pattern, I need to bind from a row in a DataTable
(ie deep inside the visual tree) to the data context of the whole window.在使用相当标准的 MVVM 模式的 WPF 应用程序中,我需要从
DataTable
中的一行(即在可视化树的深处)绑定到整个 window 的数据上下文。 I am assuming the only way to do this is to use Mode=RelativeSource
, but it requires that I specify the AncestorLevel
.我假设这样做的唯一方法是使用
Mode=RelativeSource
,但它要求我指定AncestorLevel
。
AncestorLevel
?AncestorLevel
?AncestorLevel
, doesn't this make the code brittle?AncestorLevel
,这不会使代码变脆吗? (If I change the nesting of the visual elements, it will obviously break.) If there is no good solution involving RelativeSource
, I thought I could take an alternative approach and 'propagate' the page-level property down through the logical tree to the individual items in the list.如果没有涉及
RelativeSource
的好的解决方案,我想我可以采取另一种方法,并通过逻辑树将页面级属性向下“传播”到列表中的各个项目。 Is there an accepted pattern for this?这有一个公认的模式吗?
Has RelativeSource
ever had a way of traversing up the tree searching only by type (or am I imagining this)? RelativeSource
是否曾经有过一种仅按类型搜索树的方法(或者我在想象这个)?
See my question, similar issue was raised there:看到我的问题,那里提出了类似的问题:
WPF finding ancestor for binding WPF 寻找绑定的祖先
In short - when you give the ancestor type, it is searched upwards the elements tree until the ancestor of that given type is found (Level 1).简而言之 - 当您给出祖先类型时,它会向上搜索元素树,直到找到该给定类型的祖先(级别 1)。 Should you need to search two levels up (eg you have a grid in a grid and you're aiming for the "outer" grid), you specify
AncestorLevel=2
and the ancestor is then a second element of that particular type while traversing the elements tree.如果您需要向上搜索两个级别(例如,您在网格中有一个网格并且您的目标是“外部”网格),您指定
AncestorLevel=2
并且祖先是该特定类型的第二个元素,同时遍历元素树。
How do I determine the AncestorLevel?
如何确定 AncestorLevel?
By looking at the visual tree and figuring out the number of occurances of a specific type of parent element.通过查看可视化树并计算特定类型父元素的出现次数。 There can only be only one top-level parent window though.
虽然只能有一个顶级父 window。
Why should I need to specify this at all when I know there is only ever one window?
当我知道只有一个 window 时,为什么还要指定这个?
You don't have to.你不必。 It's perfectly fine and also very common to only specify the
AncestorType
like this:只指定这样的
AncestorType
非常好,也很常见:
{Binding SomePropertyOfTheWindow, RelativeSource={RelativeSource AncestorType=Window}}" />
If I do figure out the AncestorLevel, doesn't this make the code brittle?
如果我确实找出了 AncestorLevel,这不会使代码变脆吗? (If I change the nesting of the visual elements, it will obviously break.)
(如果我改变视觉元素的嵌套,它显然会中断。)
Yes, this is correct, but this is how you tell the binding engine to which particular item of a specific type that you want to bind to - assuming there are several to choose from.是的,这是正确的,但这就是您告诉绑定引擎要绑定到特定类型的特定项目的方式 - 假设有几个可供选择。
Although this is not binding to a Relative source, but another way of binding without the complexities of Relative and type.虽然这不是绑定到一个Relative 源,而是另一种没有Relative 和类型复杂性的绑定方式。
Another option instead of binding to a specific type of control, you can name your controls in the xaml with x:Name.另一个选项,而不是绑定到特定类型的控件,您可以在 xaml 中使用 x:Name 命名您的控件。 Then you can bind directly to that control and property on it.
然后,您可以直接绑定到该控件和其上的属性。 Ex:
前任:
<SomeControl x:Name="IWantThisControl" />
[bunch of other controls]
<YourOtherControl ThisControlProperty={Binding ElementName=IWantThisControl, Path=PropertyOnTheOtherControl}" />
It does not matter the nesting level of the controls when you use the binding to ElementName reference.使用绑定到 ElementName 引用时,控件的嵌套级别无关紧要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.