NavigationViewItem.ContainerFromMenuItem returns null #2843
rcohn
started this conversation in
Feedback wanted
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello -
I am building a WinUI3 unpackaged desktop app that uses a NavigationView. One of the features of the app is that I must force an expand or collapse of a NavigationView item under programmatic control. To do that, the only way I know of is to determine the selected item and then get the NavigationViewItem via ContainerFromMenuItem so that I can set the NavigationView's Expanded property. The NavigationView is mostly used through a collection of menu items built as an observable collection. For example, I don't add items directly to the NavigationView; I add through the item collection.
Here is a small code example that shows what I'm trying to do to expand a NavigationViewItem:
In this example, ContainerFromMenuItem returns a null so I can't complete the operation to expand the item.
However, if I call ExpandMenuItem from a NavigationView event handler (such as the _Expanding handler, which gets called when the user clicks on an item), then ContainerFromMenuItem returns a valid, non-null object.
In my example, I always get a valid non-null CNavViewItem object in the call to navView.SelectedItem. That part works fine.
But how do I ensure that ContainerFromMenuItem always returns a non-null object?
I realize what I've shown here is very patchy (and I apologize for the image, but I couldn't make the edit markup show a presentable format for the code), but hopefully there's enough for somebody to recognize what I could be doing wrong - or if in fact there is a bug in ContainerFromMenuItem. Please let me know if more information would help.
Many thanks for any assistance that can be provided for this perplexing problem.
Robert
Beta Was this translation helpful? Give feedback.
All reactions