Triggering Silverlight Prism command with a keyboard shortcut

前端 未结 2 1675
面向向阳花
面向向阳花 2021-01-14 08:57

Does anybody know whether one can trigger prism command with a shortcut? What I mean is I want to be able to define binding of a command to keyboard shortcut in declarative

相关标签:
2条回答
  • 2021-01-14 09:40

    I've created such gesture trigger. And I'd like to share it with you guys. Basically, it is System.Windows.Interactivity trigger which can parse gestures represented as strings. Usage is as simple as in ClientUI:

    <UserControl>
        <i:Interaction.Triggers>
            <behaviors:KeystrokeCommandTrigger Command="{Binding SaveChangesCommand}" Gesture="Ctrl+Shift+S" />
            <behaviors:KeystrokeCommandTrigger Command="{Binding RejectChangesCommand}" Gesture="Ctrl+Shift+R" />
            <behaviors:KeystrokeCommandTrigger Command="{Binding NewItemCommand}" Gesture="Ins" />
            <behaviors:KeystrokeCommandTrigger Command="{Binding DeleteSelectedItemCommand}" Gesture="Del" />
            <behaviors:KeystrokeCommandTrigger Command="{Binding UploadSomethingCommand}" Gesture="Ctrl+Shift+U" />
        </i:Interaction.Triggers>
    </UserControl>
    

    The code is on pastie.

    0 讨论(0)
  • 2021-01-14 09:44

    You could write an attached behavior that has a listens to the KeyUp event and then calls the Command. The complication comes in translating something like Gesture="Ctrl+Shift+A". You would need to write a parser to figure out exactly what key combination that string represents.

    0 讨论(0)
提交回复
热议问题