我有以下情况:
<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 {{1 }}
答案 0 :(得分:0)
我发现可以从任何祖先访问datacontext - 而不仅仅是声明它的DataTemplate。我想这可能归结于我对DataTemplate如何工作的误解。
这完成了这项工作:
"{Binding DataContext.PropertyOnTraPresenter,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type GroupBox}},
Converter={StaticResource boolToVis}}"