WPF:从 DataContext 访问父 DataTemplate
WPF: Access parent DataTemplate from DataContext
我有以下情况:
<DataTemplate DataType="{x:Type tra:Presenter}">
<DockPanel>
<GroupBox>
<ie:DifferentControl DataContext="{Binding LocationSelectorPresenter}"
Visibility="{Binding PropertyOnTraPresenter, Converter={StaticResource boolToVis}}"/>
</GroupBox>
</DockPanel>
</DataTemplate>
如何从具有自己的 DataContext 的对象访问 DataTemplate 数据类型的 属性?
我尝试使用 FindAncestor
如下,但没有成功。 (这来自 this similar question,但我想我遗漏了什么。)
Path=PropertyOnTraPresenter, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type tra:Presenter}}
我认为 FindAncestor
是错误的方法,因为 DataTemplate
似乎不在考虑的祖先列表中。
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried StackPanel (hash=61829898)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried ContentPresenter (hash=51997850)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried Grid (hash=51537092)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried GroupBox (hash=32646025)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried DockPanel (hash=63418642)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried ContentPresenter (hash=50027503)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried ContentPresenter (hash=37861722)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried AdornerDecorator (hash=8826739)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried Border (hash=26352004)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried SimplifierDialog (hash=6690366)
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor.......
我也看过 this question,这似乎是我的问题,但我无法使那里的解决方案合适,因为似乎不可能给 DataTemplate x:Name
.
我发现 datacontext 可以从任何祖先访问 - 而不仅仅是它在其中声明的 DataTemplate。我认为这可能是我对 DataTemplate 工作原理的误解。
这完成了工作:
"{Binding DataContext.PropertyOnTraPresenter,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type GroupBox}},
Converter={StaticResource boolToVis}}"
我有以下情况:
<DataTemplate DataType="{x:Type tra:Presenter}">
<DockPanel>
<GroupBox>
<ie:DifferentControl DataContext="{Binding LocationSelectorPresenter}"
Visibility="{Binding PropertyOnTraPresenter, Converter={StaticResource boolToVis}}"/>
</GroupBox>
</DockPanel>
</DataTemplate>
如何从具有自己的 DataContext 的对象访问 DataTemplate 数据类型的 属性?
我尝试使用 FindAncestor
如下,但没有成功。 (这来自 this similar question,但我想我遗漏了什么。)
Path=PropertyOnTraPresenter, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type tra:Presenter}}
我认为 FindAncestor
是错误的方法,因为 DataTemplate
似乎不在考虑的祖先列表中。
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried StackPanel (hash=61829898)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried ContentPresenter (hash=51997850)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried Grid (hash=51537092)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried GroupBox (hash=32646025)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried DockPanel (hash=63418642)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried ContentPresenter (hash=50027503)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried ContentPresenter (hash=37861722)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried AdornerDecorator (hash=8826739)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried Border (hash=26352004)
System.Windows.Data Warning: 73 : Lookup ancestor of type tra:Presenter: queried SimplifierDialog (hash=6690366)
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor.......
我也看过 this question,这似乎是我的问题,但我无法使那里的解决方案合适,因为似乎不可能给 DataTemplate x:Name
.
我发现 datacontext 可以从任何祖先访问 - 而不仅仅是它在其中声明的 DataTemplate。我认为这可能是我对 DataTemplate 工作原理的误解。
这完成了工作:
"{Binding DataContext.PropertyOnTraPresenter,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type GroupBox}},
Converter={StaticResource boolToVis}}"