wpf - 使用 WindowsFormsHost 托管控件的 ElementHost 托管 WPF View 的键盘焦点导航问题

标签 wpf winforms wpf-controls windowsformshost elementhost

托管 WPF 用户控件的 Windows 窗体应用程序存在键盘焦点问题。当我按下 Tab 键时,如果 UserControl 中只有 WPF 控件,则导航效果很好。如果我将 WindowsFormsHost 托管控件添加到此 WPF UserControl,则焦点不会从 WPF UserControl 中的 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 是借用焦点,而不是归还它。

虽然这篇文章可能有所帮助:Gotchas For Working With Windows Forms/WPF Interop

Focus works differently for WPF and Windows Forms, and there were some rough edges around here that we were unable to fix.

根据 MSDN

Keyboard interoperation relies on implementing the OnNoMoreTabStops method to handle TAB key and arrow key input that moves focus out of hosted elements

This SO question是寻找解决方法的好地方。

关于wpf - 使用 WindowsFormsHost 托管控件的 ElementHost 托管 WPF View 的键盘焦点导航问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6183582/

相关文章:

c# - WPF 中可调整大小的自定义窗口

c# - 像 Google Chrome 选项卡一样的选项卡控件?

c# - 在我的自定义文本框中读取表单中的所有文本框

wpf - MVVM:设计具有聚合/依赖 ViewModel 的 ViewModel 架构

c# - 如何使用 WPF 为 2D 迷宫实现 "fog of war"效果

c# - 如何在左侧或右侧以不同比例向 MSChart 添加更多 Y 轴

wpf - 自定义与用户控制

wpf - 如何设置 WPF 窗口的位置?

wpf - Generic.xaml 有什么特别之处?

c# - TabControl C# 中的图标 - 如何?