WPF Prism Key事件问题

时间:2010-04-20 13:17:00

标签: c# wpf prism onkeyup

我无法解决以下问题。

我有2个模块/视图(A& B)。

在模块A上我有一个列表框,其中包含项目1-4。每次按“Enter”键打开模块B时,我都会触发一个按键事件,此事件位于包含列表框的网格上。

模块B上的

我有一个关闭模块B并打开模块A的按钮。我在此控件上设置的唯一属性是IsDefault = true。

当我按下'Enter'时,模块B关闭但BUT模块A现在也捕获了Key Up事件,导致无限循环的“Enter”键被按下。

我现在已经为此奋斗了2天,所以任何帮助都会受到赞赏。

以下代码示例:

模块A - .xaml

<UserControl x:Class="Module1.UxModule1"
             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" d:DesignHeight="700" d:DesignWidth="800">
      <Grid Focusable="False">
        <Grid Name="MainGrid"  KeyUp="MainGrid_KeyUp" Loaded="UserControl_Loaded">
            <ListBox Grid.Column="0" Width="Auto" Foreground="White" Background="Black" Height="520" BorderThickness="0" Name="lstMenuLeft" IsTabStop="True" IsSynchronizedWithCurrentItem="False" IsTextSearchEnabled="False"></ListBox>
             </Grid>
        </Grid>
</UserControl>

模块A - .cs

    private void MainGrid_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            if (lstMenuLeft.SelectedItems.Count > 0)
            {
                MessageBox.Show(lstMenuLeft.SelectedItem.ToString());
                OpenModuleB();
            }
        }
     }

    //standard prism code to inject new view.
    private void OpenModuleB()
    {

        var regionManager = _container.Resolve<IRegionManager>();
        var view = regionManager.Regions["Main"].GetView("uxMod2");

        if (view == null)
        {
            var m = _container.Resolve<IModuleManager>();
            m.LoadModule("Mod2");
        }
        else
        {
            regionManager.Regions["Main"].Activate(view);
        }

    }

    //make sure i have focus on the listbox to allow my keyboard to move up and down.
    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        lstMenuLeft.Focus();
    }

模块B - .xaml

<UserControl 
             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" 
             d:DesignHeight="800" d:DesignWidth="600">
    <Grid Height="805" Width="Auto" Name="mainGrid" KeyUp="Grid_KeyUp_1" Background="#FFF5EFEF" Loaded="mainGrid_Loaded">
        <Label Content="Module 2 View" FontSize="24" Foreground="#FF9C03FC" Height="47" HorizontalAlignment="Left" Margin="174,12,0,0" Name="label1" VerticalAlignment="Top" Width="174" />
        <Label Content="Module 2 View" FontSize="20" Foreground="#FFFF940A" Height="41" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label2" VerticalAlignment="Top" Width="146" FontStyle="Italic" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="496,28,0,0" Name="textBox1" VerticalAlignment="Top" Width="92" />
        <Grid Focusable="True" Margin="0,84,0,457">
                <Button Content="Switch Module" Height="23" IsDefault="True" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="119" IsDefault="True"  Click="button1_Click" />
</Grid>
    </Grid>
</UserControl>

模块B - .cs

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        var regionMan = _container.Resolve<IRegionManager>();
        var prevView2 = regionMan.Regions["Main"].GetView("uxMod1");
        regionMan.Regions["Main"].Activate(prevView2);
    }

我希望这会对这个问题有所了解。

1 个答案:

答案 0 :(得分:0)

'Enter'键在OnKeyDown-event中触发Click事件。按下Enter键时模块B关闭,模块A再次激活。因此,在释放密钥时激活父表单,并获取密钥消息。

换句话说,在OnKeyDown中处理Enter键,而不是OnKeyUp。

(BTW:'Space'-key触发OnKeyUp中的点击事件。)