带有WindowsFormsHost控件的ElementHost托管WPF视图的键盘焦点导航问题

带有WindowsFormsHost控件的ElementHost托管WPF视图的键盘焦点导航问题,wpf,winforms,wpf-controls,windowsformshost,elementhost,Wpf,Winforms,Wpf Controls,Windowsformshost,Elementhost,托管WPF用户控件的Windows窗体应用程序存在键盘焦点问题。当我按下tab键时,如果UserControl中只有WPF控件,那么导航工作得很好。如果我将WindowsFormsHosted控件添加到此WPF UserControl,则焦点不会从WPF UserControl中的WindowsFormsHosted控件移开 当应用程序是WPF应用程序时,焦点导航工作得很好,但当我将此WPF UserControl添加到Windows窗体应用程序时,按TAB键不再工作 如果能在这方面得到一些帮助

托管WPF用户控件的Windows窗体应用程序存在键盘焦点问题。当我按下tab键时,如果UserControl中只有WPF控件,那么导航工作得很好。如果我将WindowsFormsHosted控件添加到此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>
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
ElementHost主机=新的ElementHost();
host.Dock=DockStyle.Fill;
host.Child=new SomeControls();
this.Controls.Add(主机);
}
}
/// 
///SomeControls.xaml的交互逻辑
/// 
公共部分类SomeControls:UserControl
{
公共控制()
{
初始化组件();
}
}

这有点棘手。本质上,托管的winform借用了焦点,但没有返回它

这篇文章可能会有帮助:

Focus对WPF和 Windows窗体,还有一些 我们在这附近的粗糙边缘 无法修复

键盘互操作依赖于 实施 方法来处理TAB键和箭头键 将焦点移出主机的输入 元素


这是一个寻找解决方法的好地方。

感谢您向员工传授聚焦知识。使用OnNoMoreTabStops方法确实解决了这个问题。我也将这个问题发布到MSDN论坛,他们找到了解决我问题的方法。以下是MSDN论坛帖子的链接:

感谢您的回答。这里有很好的链接。我已经检查了其中的一些链接,但我不明白为什么当应用程序是WPF应用程序时,焦点不会从托管文本框中消失。