我在承载WPF UserControls的Windows窗体应用程序中遇到键盘焦点问题。当我按下tab键时,如果UserControl中只有WPF控件,导航就会很好地工作。如果将承载WindowsFormsHost的控件添加到此WPF UserControl,则焦点不会从WPF User控件中的WindowsFormsHosted控件移开。
当应用程序是WPF应用程序时,焦点导航可以很好地工作,但当我将此WPF UserControl添加到Windows窗体应用程序中时,按TAB键就不再工作了。
如果能得到一些帮助就太好了。
这是我的代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ElementHost host = new ElementHost();
host.Dock = DockStyle.Fill;
host.Child = new SomeControls();
this.Controls.Add(host);
}
}
/// <summary>
/// Interaction logic for SomeControls.xaml
/// </summary>
public partial class SomeControls : UserControl
{
public SomeControls()
{
InitializeComponent();
}
}
<UserControl x:Class="TabAndHostTest.SomeControls"
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:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:my="clr-namespace:TabAndHostTest" Width="450">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="124*" />
<ColumnDefinition Width="388*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="value1" Height="28" HorizontalAlignment="Left" Name="value1" VerticalAlignment="Top" />
<TextBox Grid.Row="0" Grid.Column="1" Height="23" HorizontalAlignment="Left" Name="textBox1" VerticalAlignment="Top" Width="257" />
<Label Grid.Row="1" Content="value2" Height="28" HorizontalAlignment="Left" Name="value2" VerticalAlignment="Top" />
<TextBox Grid.Row="1" Grid.Column="1" Height="23" HorizontalAlignment="Left" Name="textBox2" VerticalAlignment="Top" Width="257" />
<Label Grid.Row="2" Grid.Column="0" Content="hostedvalue1" Height="28" HorizontalAlignment="Left" Name="hostedvalue1" VerticalAlignment="Top" />
<WindowsFormsHost Grid.Column="1" Grid.Row="2" Height="23" HorizontalAlignment="Left" Name="windowsFormsHost1" VerticalAlignment="Top" Width="307">
<forms:TextBox x:Name="formsTextbox1" Height="23" Width="150" />
</WindowsFormsHost>
<Label Grid.Row="3" Grid.Column="0" Content="hostedvalue2" Height="28" HorizontalAlignment="Left" Name="hostedvalue2" VerticalAlignment="Top" />
<WindowsFormsHost Grid.Column="1" Grid.Row="3" Height="23" HorizontalAlignment="Left" Name="windowsFormsHost2" VerticalAlignment="Top" Width="307">
<forms:TextBox x:Name="formsupdown1" Height="23" Width="150" />
</WindowsFormsHost>
</Grid>
这有点棘手。从本质上讲,托管的winform是借用焦点,但不是归还焦点
看看这篇文章可能会有所帮助:使用Windows Forms/WPF Interop 的窍门
Focus对WPF和Windows窗体我们在这里的粗糙边缘无法修复。
根据MSDN
键盘互操作依赖于实现OnNoMoreTabStops处理TAB键和箭头键的方法将焦点移出宿主的输入元件
这个SO问题是寻找变通办法的好地方。
感谢Mark Staff对聚焦的了解。使用OnNoMoreTabStops方法确实解决了此问题。我也把这个问题发布到MSDN论坛上,他们找到了我问题的解决方案。以下是MSDN论坛线程的链接:http://social.msdn.microsoft.com/Forums/en-ZA/wpf/thread/054d8509-dd2d-4b60-9b0a-383b0147e2ac