0

I have the below situation:

<DataTemplate DataType="{x:Type tra:Presenter}">
        <DockPanel>
            <GroupBox>

                    <ie:DifferentControl DataContext="{Binding LocationSelectorPresenter}" 
                    Visibility="{Binding PropertyOnTraPresenter, Converter={StaticResource boolToVis}}"/>

            </GroupBox>
        </DockPanel>
    </DataTemplate>

How do I access a property on the DataTemplate's datatype, from something that has it's own DataContext?

I've attempted using FindAncestor as below, but with no luck. (This came from this similar question, but I presume I'm missing something.)

Path=PropertyOnTraPresenter, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type tra:Presenter}}

I presume FindAncestor is the wrong approach here, as DataTemplate doesn't appear to be in the list of ancestor's considered.

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.......

I've also looked at this question, which seems to be my problem in reverse, but I can't make the solutions there fit, as it doesn't seem possible to give a DataTemplate an x:Name.

Community
  • 1
  • 1
Chris
  • 5,882
  • 2
  • 32
  • 57
  • 1
    FindAncestor finds you only objects which are in the control tree, but the datacontext-object is not there. For me it's not clear what you are trying to achieve. The DataTemplate is for the type "Presenter", so you have all properties available of that type, why do you want to walk up the controltree ? – Martin Moser Feb 20 '15 at 15:34
  • Hi @Martin - The DataContext on `ie:DifferentControl` was preventing me from accessing variables in `Presenter` - I believe. I've just figured out a work around, which I'll post shortly - perhaps my problem might make more sense then. Thanks for your eyes, however. – Chris Feb 20 '15 at 15:38

1 Answers1

0

I found the datacontext could be accessed from any of the ancestors - and not just the DataTemplate it was declared in. I think this may be down to my misunderstanding of how a DataTemplate works.

This did the job:

"{Binding DataContext.PropertyOnTraPresenter, 
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type GroupBox}}, 
Converter={StaticResource boolToVis}}"
Chris
  • 5,882
  • 2
  • 32
  • 57