使用Prism / Mvvm / MEF区域的自定义命令的键盘快捷键

时间:2015-01-08 12:16:30

标签: wpf mvvm prism

我无法分辨我的问题所在 - 我怀疑我缺少一些非常的基本技术。

以下是标签和命令的问题。 我正在尝试一些基本的东西 - 使用菜单,工具栏和通用数据输入'控件'。

应用程序启动后,我只能选中到MainToolbar - 这没关系。 'OpenProjectCommand'调用一个方法,用一些东西填充一般数据。我的期望是我之后可以选择一般数据 - 但它没有标签。焦点保留在工具栏中,在打开和关闭按钮之间切换。

其次,在应用程序启动后,快捷键的键绑定工作 - 这没关系。 在调用OpenProjectCommand方法之后,我点击进入ProjectData(因为我不能在那里标签)然后只有CTLR-O工作。 CTRL-C不起作用。 如果我将焦点移回菜单或工具栏,则CTRL-C可以正常工作。

我正在使用Prism / DelegateCommand。使用Debug,当代码不起作用时,我从未到达canCloseProject()方法 - 所以我不知道内部是什么状态。

有没有办法为键盘事件设置断点? (不添加一堆代码)

或者仅仅是因为我错过了一个细节而且对于WPF / Prism太过盲目/太新了看不到它?

以下是代码:

的App.xaml

<Application x:Class="TestMenu.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

         StartupUri="View/MainWindow.xaml">
    <Application.Resources>         
    </Application.Resources>
</Application>

MainWindow.xaml

<Window x:Class="TestMenu.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:view="clr-namespace:TestMenu.View"
    xmlns:viewmodel="clr-namespace:TestMenu.ViewModel"

    Title="{Binding MyTitle}" Height="350" Width="525" WindowStartupLocation="CenterScreen" >

    <StackPanel>
        <view:MainMenuView />
        <view:MainToolbarView />
        <view:MainProjectDataView />
    </StackPanel>
    <Window.InputBindings>
        <KeyBinding Key="O" Modifiers="Control" Command="{Binding Path=OpenProjectCommand}" />
        <KeyBinding Key="C" Modifiers="Control" Command="{Binding Path=CloseProjectCommand}" />
    </Window.InputBindings>
    <Window.DataContext>
        <viewmodel:MainWindowViewModel />
    </Window.DataContext>    
</Window>

MainMenuView.xaml

<UserControl x:Class="TestMenu.View.MainMenuView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:cmd="clr-namespace:TestMenu.ViewModel"            
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel   Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Top">
        <Menu IsMainMenu="True" Height="20" Visibility="Visible">
            <MenuItem Header="_Project" MaxWidth="50">
                <MenuItem Name="Project_Open" Header="Open"  ToolTip="Open a project."  Command="{Binding OpenProjectCommand}" InputGestureText="CTRL+O" />
                <Separator></Separator>
                <MenuItem Name="Project_Close" Header="Close" ToolTip="Close the project." Command="{Binding CloseProjectCommand}" InputGestureText="CTRL+C" />
            </MenuItem>
        </Menu>
    </StackPanel>
</UserControl>

MainToolbarView.xaml

<UserControl x:Class="TestMenu.View.MainToolbarView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:cmd="clr-namespace:TestMenu.ViewModel"

         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" Width="700">

    <UserControl.Resources>
        <Style x:Key="ImageEnabled" TargetType="Image">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Opacity" Value="0.25"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
        <ToolBar HorizontalAlignment="Left">
            <Button Name="Open" ToolTip="Öffnet ein Projekt." Command="{Binding OpenProjectCommand}">
            <Image Source="/Images\Open.png" Height="32" Width="32" OpacityMask="Black" />
            </Button>
            <Button Name="Save" ToolTip="Speichert das aktuelle Projekt." Command="{Binding CloseProjectCommand}">
                <Image Source="/Images\Save.jpg" Height="32" Width="32" Style="{StaticResource ImageEnabled}" />
            </Button>
        </ToolBar>
    </StackPanel>
</UserControl>

MainProjectDataView.xaml

<UserControl x:Class="TestMenu.View.MainProjectDataView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

         mc:Ignorable="d" >
    <UserControl.InputBindings>
        <KeyBinding Key="O" Modifiers="Control" Command="{Binding Path=viewmodels.OpenProjectCommand}" />
        <KeyBinding Key="C" Modifiers="Control" Command="{Binding Path=viewmodels.CloseProjectCommand}" />
    </UserControl.InputBindings>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <DockPanel Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Stretch" Width="Auto" LastChildFill="True">
            <Label Content="Project:" Height="28" Name="Projectlabel" />
            <TextBox Height="23" Name="ProjecttextBox" Width="Auto" Text="{Binding MyTitle}"/>
        </DockPanel>
        <TextBox Grid.Row="1" AcceptsReturn="True" Text="{Binding MyTextData}"></TextBox>
        <DataGrid Name="dgMainProjectDataGrid" Grid.Row="2" 
              ItemsSource="{Binding MainProjectDataTable}" 
              AutoGenerateColumns="True"
              SelectionMode="Extended"
              SelectionUnit="Cell"
              FrozenColumnCount="1"
              IsSynchronizedWithCurrentItem="True"
              CanUserAddRows="False"/>
    </Grid>        
</UserControl>

0 个答案:

没有答案