The keyboard inputs are not updating correctly in the hosted WinForms application

Yathavakrishnan 0 Reputation points
2025-02-04T09:07:39.76+00:00

Hi,

We are encountering an issue in a WPF application hosted within a WinForms application. The keyboard event is triggered correctly, but the values are not updating properly in the UI.

 

Here’s the setup:

  1. Create a WinForms application with a user control.
  2. Create a WPF application with two XAML files.
  3. In the WinForms user control, add an element host for the WPF control.
  4. In the WPF control’s code-behind, create an instance of the main window and display it using button click.
  5. The main window has a TextBox, but when attempting to type letters or numbers in key board, the TextBox does not update in the UI.
public partial class ReportHost : UserControl
{
    private ElementHost _elementHost;
    public ReportHost()
    {
        InitializeComponent();
        var wpfControl = new WpfControl();
        _elementHost = new ElementHost()
        {
            Dock = DockStyle.Fill,
        };
        _elementHost.Child = wpfControl;
        Controls.Add(_elementHost);
    }
}
<UserControl
    x:Class="ShowReport.WpfControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:ShowReport"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <Grid>
        <Button Click="ButtonBase_OnClick" Content="Show PDF Viewer" />
    </Grid>
</UserControl>

public partial class WpfControl : UserControl
{
    public WpfControl()
    {
        InitializeComponent();
    }
    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        var window = new MainWindow();
        window.Show();
    }
}

<Window
    x:Class="ShowReport.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:ShowReport"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <TextBox Height="100" Width="200"/>
</Window>

Could you kindly check the code snippet and suggest a solution to resolve this issue?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,922 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,822 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,400 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,269 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
10,650 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 48,861 Reputation points Microsoft Vendor
    2025-02-04T09:20:44.63+00:00

    Hi @Yathavakrishnan , Welcome to Microsoft Q&A,

    Call the ElementHost.EnableModelessKeyboardInterop method before showing a new WPF window to enable keyboard interoperation between WinForms and WPF.

    using System.Windows.Forms.Integration; 
    
    public partial class WpfControl : UserControl
    {
        public WpfControl()
        {
            InitializeComponent();
        }
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var window = new MainWindow();
            ElementHost.EnableModelessKeyboardInterop(window);
            window.Show();
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.