Open vgromfeld opened 5 years ago
Conceptually, NavigationView was designed to hold navigation destinations (usually pages within an app) rather than traditional commands (like Save, Open, Copy). The Back button is different from Settings and other NavigationViewItems in that its purpose is to issue a "navigate backwards in the stack" command.
Your sample code is implying that nav view items all have a "navigate to page x" command. So this seems like a good feature request.
Sorry for the necro, but I was just looking for this feature. I'd also add that if a Command capability is added, a CommandParameter should be usable as well. For me, the commands would be especially useful in the footer, eg. If I want to have a print Item there, that needs to do more work than just change the selection maybe. A command would feel cleaner than using the "Tapped"-Event in MVVM.
Would love to see this change request implemented.
Conceptually, NavigationView was designed to hold navigation destinations (usually pages within an app) rather than traditional commands (like Save, Open, Copy).
This is one of the most short-sighted design decisions in all of UWP/WinUI. There is no difference between a 'navigation destination' and a command in MVVM. The design in the OP is the way every control should be built.
Just stumbled across this and i am baffled that there are still controls around that do not support command binding. Seemed natural to me that i could just bind those NavigationViewItem objects to commands in the viewmodel, but nope this is still not implemented.
I would love to see this implemented, particularly as I just wasted over an hour trying to figure out how to do it since it's the model/approach used by most/all of the other controls :)
My solution was to set SelectsOnInvoked
to false
, now it is no longer not selected when you click the element.
With the event PointerPressed
you can do something when the element is pressed.
Proposal: Update NavigationView to support command binding
Summary
NavigationView
is an useful navigation control but it does not expose any command properties to easily bind the back button, navigation item or settings button action toICommand
.Rationale
XAML has been designed with the MVVM pattern in mind and rely a lot on binding to allow the view models to drive the view. The most common way to bind actions is to use objects implenting
ICommand
.This pattern is available in (almost) all the available relevant controls (buttons, command bars...). If we want to use
NavigationView
we have no other choice than to write some custom code behind to route theNavigationView
raw event to view models commands.This creates an unnecessary work that can be handled by the compiler ({x:Bind}) or the runtime ({Binding}) (as for all the other buttons).
Scope
NavigationView.BackCommand
to bind a customICommand
on theBackRequested
eventNavigationView.SettingsCommand
to bind a customICommand
on theItemInvoked(IsSettings=true)
eventNavigationViewItem.Command
on the items to bind a customICommand
when a navigation item is invoked`ICommand.CanExecute()
returned value fromNavigationView.BackCommand
to drive the back button enabled stateICommand.CanExecute()
returned value fromNavigationView.SettingsCommand
to drive the settings button enabled stateImportant Notes
With this proposal, the usage could become:
This would avoid us writing code like the following one where we need to reconciliate the invoked item and its command using some error prone code.