0
Im試圖解決以下問題時遇到問題。WPF Prism Key事件問題
我有2個模塊w/views(A & B)。
在模塊A我有一個列表框項目1-4。我有一個按鍵事件,每次按下'Enter'鍵打開模塊B,這個事件在包含列表框的網格上。
模塊B我有一個按鈕,關閉模塊B並打開模塊A.我在此控件上設置的唯一屬性是IsDefault = true。
當我按下'Enter'時,模塊B關閉但是模塊A現在還捕獲了鍵盤事件,這導致無限循環的'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);
}
我希望這個問題能夠解決一些問題。
某些代碼示例將是有益的,但你可能要考慮Handled屬性的EventArgs對象發送到您的事件處理器代碼。 – juharr 2010-04-20 13:41:36