236

I have a list (see below) contained in a window. The window's DataContext has two properties, Items and AllowItemCommand.

How do I get the binding for the Hyperlink's Command property needs to resolve against the window's DataContext?

<ListView ItemsSource="{Binding Items}">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="Action">
        <GridViewColumn.CellTemplate>
          <DataTemplate>
            <StackPanel>
              <TextBlock>

                <!-- this binding is not working -->
                <Hyperlink Command="{Binding AllowItemCommand}"
                           CommandParameter="{Binding .}">
                    <TextBlock Text="Allow" />
                </Hyperlink>

              </TextBlock>
            </StackPanel>
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
    </GridView>
  </ListView.View>
</ListView>
Daniel
  • 10,864
  • 22
  • 84
  • 115
Jordan
  • 2,811
  • 2
  • 20
  • 21
  • Can you get into the debugger and step through to the point where the UI is being built? If so can you get into the variable and try to drill up – ist_lion Jul 14 '09 at 20:49
  • Simple solution (which also works in Windows 8 Store/Metro app) is here: [http://stackoverflow.com/questions/15366609/how-to-access-parents-datacontext-in-window-8-store-apps/15419382#15419382](http://stackoverflow.com/questions/15366609/how-to-access-parents-datacontext-in-window-8-store-apps/15419382#15419382) – LMK Jun 13 '13 at 03:18

3 Answers3

463

You could try something like this:

...Binding="{Binding RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type Window}}, Path=DataContext.AllowItemCommand}" ...
flq
  • 22,247
  • 8
  • 55
  • 77
  • 4
    I used this to bind to a ICommand on my VM from a ContextMenu set on a ListBoxItem from within a Style. Worked great, thanks!! – Wil P Nov 02 '11 at 17:13
  • 1
    How does one do this in a Windows 8 app? I used "ElementName=..." to get it to work, but it leaks the DataContext – Joris Weimar Jan 22 '13 at 11:26
  • Sadly didn't work for me because the parent is in a different file. – Thomas Dec 20 '15 at 09:38
  • @Thomas6767 , Hope you have solved your issue can your please your code. – Mohammed Abrar Ahmed Aug 20 '16 at 07:23
  • @MohdAbrarAhmed Could take a while, but I'll dig it up this weekend. – Thomas Sep 30 '16 at 09:30
  • 1
    Thanks! I was confusing myself with a relative binding because I was thinking it would give me the DataContext of the ancestor, not the ancestor itself. Adding the DataContext. solved the problem :) – DrEsperanto May 06 '19 at 15:12
53

This will also work:

<Hyperlink Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},
                             Path=DataContext.AllowItemCommand}" />

ListView will inherit its DataContext from Window, so it's available at this point, too.
And since ListView, just like similar controls (e. g. Gridview, ListBox, etc.), is a subclass of ItemsControl, the Binding for such controls will work perfectly.

Yoda
  • 539
  • 1
  • 9
  • 18
Kylo Ren
  • 8,551
  • 6
  • 41
  • 66
10

This also works in Silverlight 5 (perhaps earlier as well but i haven't tested it). I used the relative source like this and it worked fine.

RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=telerik:RadGridView}"

sangers
  • 176
  • 1
  • 6