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

Yathavakrishnan 40 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?

Developer technologies | Windows Forms
Developer technologies | Windows Presentation Foundation
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
Developer technologies | C#
Developer technologies | 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.
{count} votes

Answer accepted by question author
  1. Anonymous
    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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