DataGrid DataTemplate Parent DataContext

Doug 1 Reputation point

I have a DataGrid that is using a ComboBox in a DataTemplate.

I'm following MVVM pattern for my UWP app. My Xaml page has a view model attached.

The DataGrid is x:Bind'ing to a list of models (AppModels) in the view model.

The ComboBox in the DataGrid needs access to another list of items in the view model (not the AppModel context).

How can I reference the view model on the Xaml page from the DataTemplate? The Xaml docs mention using RelativeSource and AncestorType, but that does not seem to be available in UWP.

BTW - I tried to post a code snippet, but nothing but whitespace was rendered.

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Richard Zhang-MSFT 6,926 Reputation points


    Welcome to our Microsoft Q&A platform!

    When you set the DataGrid.ItemsSource, it is equivalent to restricting the DataContext of the DataGrid.Columns. You cannot bind beyond the scope of the current DataContext.