开发者

Silverlight Access Key Shortcuts

I need to provide access key shortcut for several functions such as Save. To do this I have started by handling the KeyUp event of my root object which is a Grid called LayoutRoot (typically created as default in a Silverlight UserControl or Page).

I am using an MVVM pattern but for this I have added code in the code behind as such (this is UI interaction so it seems OK):

        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {开发者_JS百科
           switch (e.Key)
           {
               case Key.S:
                   if ((Keyboard.Modifiers & ModifierKeys.Windows) == ModifierKeys.Windows)
                   {
                       e.Handled = true;
                       // save

                   }
                   break;

                case Key.C:
                   if ((Keyboard.Modifiers & ModifierKeys.Windows) == ModifierKeys.Windows)
                   {
                       e.Handled = true;
                       // clear fields

                   }
                   break;

           }
       }

I have used the Windows key because there are no shorcuts in the browser that use it as far as I know.

I implement the Save functionality on the Save button using a Command, therefore maintaining the MVVM pattern. E.g.

public RelayCommand CommandSavePtr { get; private set; }
CommandSavePtr = new RelayCommand(OnSavePtr);
private void OnSavePtr()
{
  ....

In XAML: -

 <Button x:Name="SavePtrButton" 
                        Command="{Binding CommandSavePtr}" 
                        Style="{StaticResource StandardButtonStyle}" 
                        IsEnabled="{Binding Ptr.HasErrors, Converter={StaticResource NotOperatorValueConverter}}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/G4SPrisonerEscorting_ResourceDictionaries;component/images/accept.png" Style="{StaticResource SubPanelIconStyle}"/>
                        <TextBlock Text="Save"/>
                    </StackPanel>
 </Button>

My problem now is that I don't know how to communicate to the ViewModel from my above KeyUp event to perform the same Save function that is perfomed when clicking the Save button.

Could anyone point me in the right direction.

BTW I am using GalaSoft's MVVM Light to do the Commanding.


This is an old question, but I thought I would answer it anyway if someone else stumbles upon the same problem.

It should be possible to fire the command for the button in the code-behind in the following manner:

SavePtrButton.Command.Execute(SavePtrButton.CommandParameter);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜