简体   繁体   English

WPF 使用 WPF ElementHost 仅在 Win 7 下具有鼠标滚轮问题的控件托管 Winforms

[英]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.我有一个与 Bing 地图一起使用的 WPF ElementHost。 Everything works great in Windows 10 , but zooming with the MouseWheel will not work in Windows 7 .Windows 10中一切正常,但在Windows 7中无法使用鼠标滚轮进行缩放。

I have tried making sure the hosted element has focus by adding an event handler for UCBingMap.MouseEnter and calling UCBingMap.Focus .我尝试通过为UCBingMap.MouseEnter添加事件处理程序并调用UCBingMap.Focus来确保托管元素具有焦点。 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...仍然 - 它在 Windows 7 下不起作用,但在 Windows 10 下起作用。可悲的是 - 在可预见的未来,我有很多客户坚持使用 Win7...

My XAML for the user control我的 XAML 供用户控制

<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).事实证明,同一表单上的另一个控件正在从 WPF ElementHost 中窃取 MouseWheel 事件(在我的情况下,它托管了一个名为 BingMapUC 的 BingMap)。 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.它只在 Windows 7 (SP1) 下执行,但不在 Windows 10 下执行。我能够通过手动为元素添加处理程序来解决我的 VB.NET 源中的问题。

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在那种情况下,我手动将鼠标控件从 ListView 控件(那是窃取 MouseWheel 事件的那个)

lsvControl.Capture = False

and then I make sure the BingMapUC (the hosted WPF) gets the focus然后我确保 BingMapUC(托管的 WPF)获得焦点

BingMapUC.Focus()

This works under both Windows 10 and Windows 7 so I don't have to check.这在 Windows 10 和 Windows 7 下都有效,所以我不必检查。

I did also make sure I gave mouse control back to the listview on its MouseEnter event with我还确保在其 MouseEnter 事件中将鼠标控制权交还给列表视图

lsvControl.Capture = True

So it would scroll when the mouse was over it.所以当鼠标在它上面时它会滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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