Leveraging x:Bind with events
In the previous chapter, we bound view model commands to the Command
properties of the Add and Delete buttons. This works great and keeps the view model decoupled from the UI, but what happens if you need to handle an event that isn't exposed through a Command
property? For this scenario, you have two options:
- Use
Binding
in the view to bind to a command on the view model. - Use
x:Bind
in the view to bind directly to an event handler on the view model.
If you are planning to share view models between WinUI and WPF, binding to commands is recommended because both app models support the Binding
syntax. If your view models are only going to be used in a WinUI application, you should use x:Bind
. This option will provide compile-time type checking and added performance.
We want to give users of the My Media Collection
application the option to double-click (or double-tap) a row on the list to view or edit its details. The new Item Details...