WinRT XAML Databinding: how to bind to a property in the context of parent data when binding in ItemTemplate?

I have a GridView for which I programmatically set the data context to the view model instance. The GridView ItemsSource is bound to the observed collection (PagesToRead), which is a property in the view model.

In GridView.ItemTemplate, the binding goes against the observed collection in the ItemsSource , but I want to bind the StackPanel Background element to another property in the view model.

I am looking for magic <Background="{Binding Path=BackgroundColor, Source=???}">that will exit the current ItemsSource and bind to the BackgroundColor property in the view model.

Here's the skipped XAML:

<Grid>
  <GridView x:Name="MainGrid" CanReorderItems="True" CanDragItems="True" 
    ItemsSource="{Binding Path=PagesToRead}"
    <GridView.ItemTemplate>
      <DataTemplate >
          <StackPanel>
            <Background="{Binding Path=BackgroundColor, Source=???}">
            <TextBlock Text="{Binding Path=Title}" 
          </StackPanel>
      </DataTemplate>
    </GridView.ItemTemplate>
  </GridView>
</Grid>
+1
source share
1 answer

I got the answer through another prospectus (thanks to Karl Erickson). What are you doing:

<StackPanel Background="{Binding Path=DataContext.TileBackgroundColor,
                         ElementName=MainGrid">
+1
source

Source: https://habr.com/ru/post/1584149/


All Articles