1

I have a User Control which serves as DataTemplate in page. There is a response for similar problem in WP8 - access datacontext of parent but there DataTemplate is defined "inside" ItemsControl. It does not work in scenario like this:

<Grid Name="layoutRootGrid">
        <ListView Name="listViewParent" HorizontalAlignment="Left" Height="458" Margin="42,24,0,0" VerticalAlignment="Top" Width="298" 
                  ItemsSource="{Binding ListViewSource}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <controls:ListViewControl />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>

While "ListViewControl" is (ListViewSource and ParentProp are collection and property from bound ViewModel - DataContext of listViewParent):

<Grid Background="Black" Name="templateGrid">
            <TextBlock Text="{Binding DataContext.ParentProp, ElementName=listViewParent, FallbackValue='couldnt get parent prop'}" VerticalAlignment="Top"/>
    </Grid>
Community
  • 1
  • 1
eu-saro
  • 11
  • 1
  • 5
  • you did not use x:key=ListViewDataTemplate anywhere so you can simply remove it. A small working example wich show your problem would be nice. – blindmeis Apr 23 '15 at 13:25
  • I removed unnecessary code and here is link for sample app (WP8.1 universal app, VS2013) http://s000.tinyupload.com/download.php?file_id=00236498195899613726&t=0023649819589961372697368 – eu-saro Apr 23 '15 at 14:00

2 Answers2

3

Pass ParentProp through the Tag property of the user control

MainPage.xaml:

<Grid Name="layoutRootGrid">
    <ListView Name="listViewParent"
                VerticalAlignment="Top"
                HorizontalAlignment="Left"
                Width="298"
                Height="458"
                Margin="42,24,0,0"
                ItemsSource="{Binding ListViewSource}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <controls:ListViewControl Tag="{Binding DataContext.ParentProp, ElementName=listViewParent}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

In your user control:

  • Set the Name property to "Self" (or whatever you want): x:Name="Self"
  • In your TextBlock, bind Text to the Tag Property of this user control

ListViewControl.xaml:

<UserControl x:Class="Namespace.ListViewControl"
                x:Name="Self"

                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid Background="Black"
            Name="templateGrid">
        <TextBlock Text="{Binding Path=Tag, ElementName=Self, FallbackValue='couldnt get parent prop'}"
                    VerticalAlignment="Top" />
    </Grid>
</UserControl>
Luzian
  • 46
  • 2
0

You should be able to bind to the parent data context like this:

<Grid Background="Black" Name="templateGrid">
    <TextBlock Text="{Binding DataContext.ParentProp, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" />
</Grid>
James Lucas
  • 2,452
  • 10
  • 15