简体   繁体   中英

WPF Control hosted Winforms using WPF ElementHost has Mouse Wheel Issue under Win 7 Only

I have a WPF ElementHost I am using with Bing Maps. Everything works great in , but zooming with the MouseWheel will not work in .中一切正常,但在中无法使用鼠标滚轮进行缩放。

I have tried making sure the hosted element has focus by adding an event handler for and calling .添加事件处理程序并调用来确保托管元素具有焦点。 I verified that it is firing. Still - it does not work under Windows 7, but does work under Windows 10. Sadly - I have plenty of customers stuck with Win7 for the foreseeable future...

My XAML for the user control

<UserControl x:Class="BingMapUC"
         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:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
         mc:Ignorable="d" 
         d:DesignHeight="393" d:DesignWidth="644" IsHitTestVisible="True" Focusable="True">
<UserControl.Resources >
    <ControlTemplate x:Key="Default" TargetType="m:Pushpin">
        <Grid x:Name="ContentGrid" HorizontalAlignment="Center" VerticalAlignment="Center">
            <StackPanel>
                <Grid Margin="0" Width="24" Height="23">
                    <Rectangle HorizontalAlignment="Left"  Margin="0,3.238,0,-2.146" Width="23" Height="18" Fill="SteelBlue" Stroke="Black"  RenderTransformOrigin="0.5,0.62">
                        <Rectangle.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform AngleY="0" AngleX="-23"/>
                                <RotateTransform Angle="123"/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </Rectangle.RenderTransform>
                    </Rectangle>

                    <Rectangle Fill="{TemplateBinding Background}" Stroke="Black" RadiusX="5" RadiusY="5"/>

                    <ContentPresenter HorizontalAlignment="Center"
                                                            VerticalAlignment="Center"
                                                            Content="{TemplateBinding Content}"
                                                            ContentTemplate="{TemplateBinding ContentTemplate}"
                                                            Margin="0" TextBlock.FontFamily="Segoe UI" TextBlock.FontWeight="Bold" TextBlock.Foreground="Black" >
                    </ContentPresenter>
                </Grid>
            </StackPanel>
        </Grid>
    </ControlTemplate>
</UserControl.Resources>
<Grid>
    <m:Map Name="Map" ZoomLevel="3.5" Center="38.8282,-95.5795" Cursor="Hand" CredentialsProvider="xxxxxxxxxxxxxxxxxx" Height="393" UseInertia="True" Margin="0" MaxWidth="644" MaxHeight="393" MinWidth="644" MinHeight="393" ScrollViewer.VerticalScrollBarVisibility="Disabled" Width="635" ScaleVisibility="Hidden"/>
</Grid>
</UserControl>

I'm hoping I have overlooked some esoteric setting(s)...

As it turns out another control on the same form was stealing the MouseWheel events away from the WPF ElementHost (in my case it was hosting a BingMap called BingMapUC). It was only doing it under Windows 7 (SP1), but not in Windows 10. I was able to fix the problem in my VB.NET source by manually adding handlers for the ElementHost MouseEnter event.

AddHandler BingMapUC.MouseEnter, AddressOf BingMap_MouseEnter

In that event I manually take the mouse control away from the ListView control (that was the one stealing the MouseWheel events) with

lsvControl.Capture = False

and then I make sure the BingMapUC (the hosted WPF) gets the focus

BingMapUC.Focus()

This works under both Windows 10 and Windows 7 so I don't have to check.

I did also make sure I gave mouse control back to the listview on its MouseEnter event with

lsvControl.Capture = True

So it would scroll when the mouse was over it.

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