Search code examples
c#xamluwpuwp-xamlwindows-community-toolkit

How can you deep-link into an item in a MasterDetailsView on devices with small screens?


I have a MasterDetailsView on a page which I'm navigating to from another page. I pass in an argument which corresponds to an item in the list the MasterDetailsView is bound to as the ItemSource, like this:

protected override void OnNavigatedTo(NavigationEventArgs e) {
 if (e.Parameter != null) {
  Thing selectedThing = e.Parameter as Thing;
  Selected = selectedThing;
  MasterDetailsViewControl.SelectedItem = selectedThing;
 }
}

This works fine on form factors with large screens - the DetailsView populates successfully with the relevant content. However, on smaller screens, the DetailsView is hidden in favour of the ListView being full screen. This is normally advantageous, but here I want to have the DetailsView open, as an individual Thing has just been deep linked to.

Seeing as the ViewState property of a MasterDetailsView is read-only, is there some way to force the DetailsView open rather than the ListView on these smaller screens? If not, how should one go about facilitating that deep linking functionality?


Solution

  • With my testing, When the MasterDetailsView control has not been loaded, if you set the SelectedItem property, it will not fire the OnSelectedItemChanged handler. That's the reason why the DetailsView will not open.

    To solve your question, you could set the SelectedItem in MasterDetailsViewControl's Loaded event handler.

    private Thing Selected;
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (e.Parameter != null)
        {
            Thing selectedThing = e.Parameter as Thing;
            Selected = selectedThing;
        }
    }
    private void MasterDetailsViewControl_Loaded(object sender, RoutedEventArgs e)
    {
        MasterDetailsViewControl.SelectedItem = Selected;
    }