简体   繁体   中英

Mousewheel events in wpf control hosted in Windows forms

I have a Windows Forms application, which has one WPF user control, available through ElementHost control.

The WPF user control has code designed to respond to mouse wheel events. However none of the code written associated with these events is running in the wpf when I run the application and I use the mousewheel. I tested this by placing breakpoints in the visual basic code. Despite this, it does respond to other mouse events (such as click or drag, which I use to do rotation of my 3d model).

I was wondering that maybe the mousewheel events are not passing from the Windows Forms, to the WPF user control, because windows forms as no (or limited) mousewheel support.

In contrast, I tried to write a simple program of a WPF user control inside a WPF application, and this control responds to the mousewheel events.

The WPF usercontrol has a viewport3D, which I use to do some 3d drawing, which can't be done in Windows Forms.

Is there a workaround to this problem? I hope you can help. Thank you.

Here is an example of how I setup the Mousewheel event in the WPF control. I tried within the UserControl, Grid and Canvas, but I get no mousewheel events in either of them.

<UserControl x:Class="LPViewport3D"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" Background="white" MouseMove="UserControl_MouseMove" MouseDown="UserControl_MouseDown" MouseWheel="UserControl_MouseWheel">
<Grid MouseWheel="Grid_MouseWheel" Background="white">
    <Viewport3D x:Name="VP" Margin="0" ClipToBounds="False" Grid.Row="0" Grid.Column="0" >
<!-- more code -->

</Viewport3D>

    <!-- Ovelay canvas to receive mouse events-->
    <Canvas Grid.Row="0" Grid.Column="0" 
        Background="Transparent"
        MouseDown="Canvas_MouseDown"
        MouseMove="Canvas_MouseMove" MouseWheel="Canvas_MouseWheel" />
</Grid>

We found that you have to hook up a handler to the ElementHost.HostContainer.MouseEnter event and set the focus back to the map there. We found out how to do that from SO post: https://stackoverflow.com/a/15464539/2250424

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        elementHost1.HostContainer.MouseEnter += HostContainer_MouseEnter;
    }

    private void HostContainer_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        mapControl1.bingMap.Focus();
    }
}

I managed to resolve this problem. All I had to do was to set the control as focusable and set the focus to the control

To set the WPF user control as focusable I needed to add the entry in the XAML file

<UserControl x:Class="LPViewport3D"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         Focusable="True">

and then, in the WinForms application I needed to set the focus to the control. To ensure the control is focused and responds I also set the focus programatically in the usercontrol WPF MouseDown event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM