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

发布于 2024-11-11 15:32:13 字数 3212 浏览 0 评论 0原文

我在托管 WPF UserControls 的 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>

I have a keyboard focus problem with Windows Forms application that hosts WPF UserControls. When I press tab, the navigation works well if there are only WPF controls in the UserControl. If I add a WindowsFormsHost hosted control to this WPF UserControl, the focus is not moving away from the WindowsFormsHosted control in WPF UserControl.

When the application is WPF application, the focus navigation works perfectly but when I add this WPF UserControl to Windows Forms application, pressing TAB doesn't work anymore.

It would be great to get some help with this.

Here is my code:

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈年往事 2024-11-18 15:32:13

这有点棘手。本质上,托管 winform 正在借用焦点,但不会归还焦点。

看看这篇文章可能会有所帮助:使用时的陷阱Windows 窗体/WPF 互操作

焦点对于 WPF 和
Windows 窗体,还有一些
我们这里的边缘很粗糙
无法修复。

根据 MSDN

键盘互操作依赖
实现 OnNoMoreTabStops
处理TAB键和方向键的方法
将焦点移出托管的输入
元素

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

This is a bit tricky. Essentially, the hosted winform is borrowing the focus, but not returning it.

A look though this article might help: 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.

According to 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 is a good place to look for workarounds.

盗琴音 2024-11-18 15:32:13

感谢马克·工作人员提供的有关聚焦的知识。使用 OnNoMoreTabStops 方法确实解决了该问题。我也将这个问题发布到 MSDN 论坛,他们找到了我的问题的解决方案。以下是 MSDN 论坛主题的链接: http://social.msdn.microsoft.com/Forums/en-ZA/wpf/thread/054d8509-dd2d-4b60-9b0a-383b0147e2ac

Thank you for Mark Staff of the knowledge about focusing. The problem was really fixed with OnNoMoreTabStops method. I posted this problem to MSDN forum too and they found the solution for my problem. Here is the link to MSDN forums thread: http://social.msdn.microsoft.com/Forums/en-ZA/wpf/thread/054d8509-dd2d-4b60-9b0a-383b0147e2ac

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文