2

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.

3
  • I am not sure if I understand your issue. Are you defining the KeyBindings in XAML or programmatically? Do you want to bind the CommandParameter to the Name property of an individual item? Or what is your issue? Can't you just set the CommandProperty of the KeyBinding that you create programmatically? Commented Dec 20, 2017 at 14:57
  • I think I messed up, I was so hung up on the fact that I could not add a paramater when creating a new KeyBinding that I didn't even think about adding it after creating it. Commented Dec 20, 2017 at 15:09
  • @mm8 I want to create my KeyBindings programmatically as I have to get the data from a List, so that they can be modified by the user.The CommandParameter would be the name associated with the certail keycombination. However I think I got this fixed. Why is it always after asking for help you figure it out... Commented Dec 20, 2017 at 15:11

1 Answer 1

1

The KeyBinding class has a CommandParmeter property that you can set after you have created the object:

var kb = new KeyBinding(TestCommand, new KeyGestureConverter().ConvertFromString(modifier + "+" + key) as KeyGesture);
kb.CommandParameter = "...";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.