question

Doug-0391 avatar image
Doug-0391 asked ·

DataGrid DataTemplate Parent DataContext

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.


windows-uwpwindows-uwp-xaml
2 comments
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You can find an example of this issue here:

https://github.com/infoequipt/UwpApp

Specifically, UwpApp -> Views -> DataGridPage.


0 Votes 0 · ·

I am sorry for that inconvenience that caused for you, we are fixing this issue, currently please use these two workarounds that mentioned in this post: Can not insert XML/XAML Code in Post.


0 Votes 0 · ·

1 Answer

RichardZhang-MSFT avatar image
RichardZhang-MSFT answered ·

Hello,​

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.

Thanks.

3 comments Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the quick feedback.

I understand the scoping of the DataContext, however I would like to recommend that there be a way to escape back to the Page.

Being able to scope to parent (or Page) would allow me to load all my lookup lists into the ViewModel thus pointing the ComboBox directly to the source list.

At this point, I'm forced to include a field in each object (row) that points back to the ViewModel list.

0 Votes 0 · ·

Hi, I' sorry, but such out-of-limit bindings are not allowed in UWP. At present, you can only try to adjust the data structure to deal with this problem.

1 Vote 1 · ·

Hello, is there any new information in your question, and can we provide you with new help?

0 Votes 0 · ·