Richard Bailey
Richard Bailey

Reputation: 2738

Button(s) KeyBinding [MVVM]

I am struggling to bind some short cut keys to my WPF buttons. The buttons are built up dynamically by using the following code [only a snippet - but should be sufficient]:

// for each command defined in the database
... 
PosCommand cmd = null; // FYI: PosCommand implements ICommand
if (!string.IsNullOrEmpty(group.AssemblyPath))
{
    if (System.IO.File.Exists(group.AssemblyPath))
    cmd = (PosCommand)Activator.CreateInstance(Assembly.LoadFile(group.AssemblyPath).GetType(group.FullQualifiedName), model);
}
else
{
cmd = (PosCommand)Activator.CreateInstance(Assembly.GetExecutingAssembly().GetType(group.FullQualifiedName), model);
}
if (cmd == null)
continue;

Function function = new Function()
{
Command = cmd,
Name = group.FunctionName,
Description = group.Description,
FunctionCode = group.FunctionCode
};
...

Here is a snippet of the XAML which binds to the C# code:

<itemscontrol x:name="functionList" grid.row="0" itemssource="{Binding GroupFunctions}" xmlns:x="#unknown">
    <itemscontrol.itemtemplate>
        <datatemplate>
            <groupbox header="{Binding Group}">
                <itemscontrol scrollviewer.horizontalscrollbarvisibility="Disabled" itemssource="{Binding Functions}">
                    <itemscontrol.itemspanel>
                        <itemspaneltemplate>
                            <wrappanel />
                        </itemspaneltemplate>
                    </itemscontrol.itemspanel>
                    <itemscontrol.itemtemplate>
                        <datatemplate>
                            <Button MinWidth="91" Height="50" Content="{Binding Name}" ToolTip="{Binding Description}" Command="{Binding Command}"/> 
                        </datatemplate>
                    </itemscontrol.itemtemplate>
                </itemscontrol>
            </groupbox>
        </datatemplate>
    </itemscontrol.itemtemplate>
</itemscontrol>

I tried to add a some key binding to the button without any success?! I altered the Function class so that it contains properties for the Modifier and the Key, for each of the buttons of course. It still doesn't want to work, even if I hard code the Modifier and Key values??

<Button.InputBindings>
    <keybinding modifiers="{Binding Mod}" key="{MyKey}" />
</Button.InputBindings>

Could anyone please assist me in this matter? Many thanks in advance!

Kind regards,

Upvotes: 0

Views: 953

Answers (2)

Felice Pollano
Felice Pollano

Reputation: 33252

I would suggest to use System.Windows.Interactivity with a custom trigger. I have an example here on my blog, done for the Caliburn Micro MVVM framework, but I think it is a cross framework solution. System.Windows.Interactivity.dll is part of Blend, but it is redistributable in binary form, it does not requires any special setup, just an xcopy deploy is enough.

Upvotes: 1

Richard Bailey
Richard Bailey

Reputation: 2738

At the end of the day, I wrote my own key listener. I handled the KeyEventHandler:

protected override void OnInitialized(EventArgs e)
{            
    base.OnInitialized(e);

    this.AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)HandleKeyDownEvent);
}

private void HandleKeyDownEvent(object sender, KeyEventArgs e)
{

}

Basically, all of the ICommands are defined within the database and are built up dynamically. Thus, I made use of the CommandProperty to add a complex data type in. In this complex data type, I store a variety of information, include the short cut key to use. So the key listener waits for the key combination, looks it up and executes the ICommand.

Kind regards,

Upvotes: 0

Related Questions