I am fiddling around in C# WPF and I ran into an issue that I was unable to find a solution to.
The problem, I have a .xml file where a user can create keybindings with a value of the Sound they want to play when pressing the keybinding combination. This file gets read at start and the keybindings get added to a List and also created as a KeyBinding which then gets added to the view.
Keybindings.Add(new KeybindingsViewModel(key, modifier, sound)); //Adds it as object to my custom class.
var kb = new KeyBinding(TestCommand, new KeyGestureConverter().ConvertFromString(modifier + "+" + key) as KeyGesture); //creates a KeyBinding.
This all works fine, my command gets executed when I press the predefined keybindings, however I seem to be unable to add a parameter to the command.
What I want to be able to do is, that whenever the keys get pressed the name of the sound that should play get's send with it to the command as a param. This name is stored with the keycombination in my custom class. So if I could just add that name to the var kb (KeyBinding) at the start that would be great. I know that if I declare a Keybinding in the view.Xaml that I can add a parameter. So I assume it has to be possible to do so from the viewmodel aswell.
Example 2
My other attempt was to use the already existing ItemsSource that is used in the view to dynamically add the Sounds and add the keybinds there. Something like:
<ItemsControl x:Name="MySounds" ItemsSource="{Binding RelativeSource=sound}" >
<!--Defines the template to use a wrappanel-->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!--Inputbindings for the sounds-->
<ItemsControl.InputBindings>
<KeyBinding Key="{Binding Path=Keybind}" Modifiers="{Binding Path=Modifiers}"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.PlaySound}"
CommandParameter="{Binding Path=Name}" />
</ItemsControl.InputBindings>
So within the ItemsControl that has the collection of Sounds linked to it, which also have their associated keybindings in them, just use those values for the Keybinding. However I can't seem to get the Binding sources right, the only thing that actually goes to the correct source is the Command, which when I just put some placeholders in the Key and Modifiers does work. But it doesn't take the paramater with it, I just hate these bindings and can't ever figure out their relation.
If this is a completely crappy way of going about doing this then please let me know, this is just what I thought should be possible with my current knowledge.
Also, I am not too sure if this would break MVVM principles.