I can't find anything that looks like this online. I am looking for a way to create a collection of Keybindings in the code (with a Keybinding ViewModel), and then bind the collection to the view, instead of listing out every binding manually in Xaml.
I would expect it to look something like this
<Window.InputBindings ItemsSource="{Binding Path=KeybindingList}" />
and then in the code, have a List. Is such an approach possible? Where would I start?
You can create an attached property, listen to its changes and modify the
InputBindings
collection of the associated window.An example:
This can be used on any
UIElement
:If you want it to be very fancy you can type-check for
INotifyCollectionChanged
and update theInputBindings
if the collection changes but you will need to unsubscribe from the old collection and such so you need to be more careful with that.