Search code examples
c#xamldata-bindingwindows-store-appselementname

Listview bound via ElementName stays empty


I'm stuck again at some data-binding issue.
This time I want to bind a ListView to the SelectedItem of a GridView. I already suceed with this type of data-binding but now my ListView, which should show some details about my selected item in my GridView just stays empty. There are no items in it although they should exist.
The GridView binds just fine at the property in my MainViewModel.
Substituting the ElementName attribute with x:Resouces doesn't seem to be an option, because it doesn't work either.
The source view:

<GridView x:Name="gridViewOrderYears" 
          ItemsSource="{Binding SelectedCustOrders, Mode=TwoWay}"
          HorizontalAlignment="Left" 
          Grid.Row="1" 
          VerticalAlignment="Top" 
          Width="316" 
          Height="63" 
          Margin="657,316,0,0"
          SelectionMode="Single">
    <GridView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Border BorderThickness="1" BorderBrush="Aquamarine">
                    <StackPanel>
                        <TextBlock Text="{Binding Year}" FontSize="20"/>
                        <TextBlock Text="{Binding OrderCount}"/>
                    </StackPanel>
                </Border>
            </Grid>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

this View doesnt bind:

<ListView HorizontalAlignment="Left" 
          Height="231" 
          Margin="657,401,0,0" 
          Grid.Row="1" 
          VerticalAlignment="Top" 
          Width="316"
          DataContext="{Binding SelectedItem, ElementName=gridViewOrderYears}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DoneOrders.Order_Date, ElementName=gridViewOrderYears}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

SelectedCustOrders porperty is an IList<OrderYears>.
OrderYears is following data value object defined in my MainViewModel:

public class OrderYears
    {
        public int? Year { get; set; }
        public IList<Orders> DoneOrders { get; set; }
        public int OrderCount { get; set; }
    }

Solution

  • Finally I got it.
    After hours of trial-and-error finally substituting DataContext with ItemsSource did the trick...
    Sometimes things are easier than you think :)