WPF快捷键未触发

时间:2013-04-01 22:09:55

标签: wpf prism

我有一个prism delegate命令设置用于刷新应用程序中的数据。 我还使用委托命令为close和about窗口设置了快捷方式。

使用F5也不会触发视图模型中的刷新。如果我使用Alt + F4,应用程序将关闭。但它不会在视图模型中执行CloseApplication方法。使用Gesture Shorcuts执行ViewModel的以下代码中缺少什么?

用户控件:

<UserControl.InputBindings>
    <KeyBinding Gesture="ALT+F4" Command="{Binding CloseCommand}" />
    <KeyBinding Gesture="F5" Command="{Binding RefreshCommand}" />
</UserControl.InputBindings>
<DockPanel>
    <Menu DockPanel.Dock="Top">
        <MenuItem Header="_File">
            <MenuItem Header="E_xit" Command="{Binding CloseCommand}" InputGestureText="Alt+F4" />
            <MenuItem Header="Refresh" Command="{Binding RefreshCommand}" InputGestureText="F5" />
        </MenuItem>
        <MenuItem Header="_Help">
            <MenuItem Header="_About" Command="{Binding AboutCommand}" />
        </MenuItem>
    </Menu>
    <ToolBar ToolBarTray.IsLocked="True">
        <Button Content="Home" MinHeight="50" MinWidth="80" VerticalContentAlignment="Bottom" />
        <Button Content="Page 1" MinHeight="50" MinWidth="80" VerticalContentAlignment="Bottom" />
        <Button Content="Page 2" MinHeight="50" MinWidth="80" VerticalContentAlignment="Bottom" />
    </ToolBar>
</DockPanel>

UserControl的ViewModel

public class TopMenuViewModel:NotificationObject
    {
        private DelegateCommand _closeCommand;
        private DelegateCommand _aboutCommand;
        private DelegateCommand _refreshCommand;

        public TopMenuViewModel()
        {
            _closeCommand = new DelegateCommand(CloseApplication);
            _aboutCommand = new DelegateCommand(ShowAboutWindow);
            _refreshCommand = new DelegateCommand(RefreshApplication);
        }

        private void ShowAboutWindow()
        {
            Console.WriteLine("Show about window");
        }

        private void CloseApplication()
        {
            Application.Current.Shutdown();
        }

        private void RefreshApplication()
        {
            Console.WriteLine("Refreshing..");
        }

        public ICommand CloseCommand 
        {
            get { return _closeCommand; }
        }

        public ICommand AboutCommand
        {
            get { return _aboutCommand; }
        }

        public ICommand RefreshCommand
        {
            get { return _refreshCommand; }
        }


    }

如果我使用鼠标单击菜单或使用Alt + x或Alt + A,则会调用视图模型方法。但是没有使用我放入的KeyBinding。感谢您的帮助。

0 个答案:

没有答案