开发者

WPF Prism Key Event problem

Im having trouble trying to solve the following problem.

i have 2 modules w/views (A & B).

on module A i have a listbox with items 1-4. i have a key up event that fires everytime i press the 'Enter' key to open module B, this event is on the grid which contains the listbox.

on module B i have a button that closes module B and opens module A. the only property i've set on this control is the IsDefault = true.

When i press 'Enter' the module B closes BUT module A now captures the Key Up event as well which results in an endless loop of 'Enter' keys being pressed.

I've been struggling with this for 2 days now, so any help would be appreciated.

Code Sample below:

Module 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>

Module 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();
    }

Module 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" />
        <TextBo开发者_StackOverflow中文版x 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>

Module 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);
    }

I Hope this sheds a little more light on the problem.


'Enter'-key is triggering Click-events in OnKeyDown-event. Module B is closed when Enter key is pressed down, and Module A is activated again. So, the parent form is activated by the time the key is release and it gets the key up message.

In other words, handle Enter key in OnKeyDown, not OnKeyUp.

(BTW: 'Space'-key is triggering Click-events in OnKeyUp.)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜