WPF中的浮动自定义触摸控件 [英] Floating custom touch control in WPF

查看:35
本文介绍了WPF中的浮动自定义触摸控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 WPF 中创建一个可以接收触摸事件的浮动控件.我搜索了很多,我找到的浮动控件的唯一解决方案是 Popup 控件.问题:Popup 控件无法接收触摸事件.由于我在 Popup 中使用了 SurfaceListBox,因此我需要触摸事件.我尝试通过以下方式使弹出窗口接收触摸事件:

I am trying to create a floating control in WPF that can receive touch events. I searched a lot and the only solution for a floating control I found was the Popup control. Problem: The Popup control cannot receive touch events. And since I am using a SurfaceListBox inside my Popup, I need touch events. I tried to make the popup receive touch events via the following:

TouchExtensions.EnableSurfaceInput((HwndSource)HwndSource.FromVisual(this.selectionListPopup));

但它没有任何影响.有没有办法让弹出窗口接收触摸?有没有其他可以使用的控件或方法?

but it didn't have any effect. Is there any way to make popups receive touches? Is there any other control or way that could be used?

我的最终目标是让一个控件浮动在其他控件之上 - 这意味着它不会被其他控件剪裁或遮挡.

My end goal is to have a control that is floating above other controls - that means it is not clipped or obscured by other controls.

这是我目前使用的 XAML:

Here is the XAML I used so far:

<Popup Name="selectionListPopup"
        PlacementTarget="{Binding ElementName=selectedItemButton}"
        Placement="Relative"
        VerticalOffset="{Binding ElementName=this, Path=SelectionListTop}"
        HorizontalOffset="{Binding ElementName=this, Path=SelectionListLeft}"
        IsOpen="{Binding ElementName=this, Path=IsSelecting, Mode=TwoWay}"
        StaysOpen="False"
        AllowsTransparency="True" 
        Focusable="False">
        <s:SurfaceListBox Name="selectionList"
                          ItemsSource="{Binding ElementName=this, Path=ItemsSource}"
                          SelectedItem="{Binding ElementName=this, Path=SelectedItem, Mode=TwoWay}"
                          SelectionChanged="OnSelectionListSelectionChanged">
        </s:SurfaceListBox>
</Popup>

推荐答案

它不是 Popup 的 HwndSource,而是 Popup.Child 的可视父级的 HwndSource.听起来很奇怪,我知道.

It's not the HwndSource of the Popup, but the HwndSource of the visual parent of the Popup.Child. Sounds strange, I know.

所以在我的弹出窗口的 Opened-event 中,我调用了以下代码:

So in the Opened-event of my popup I call the following code:

var myPopup = sender as Popup;

HwndSource hwndSource = (HwndSource)PresentationSource.FromVisual((Visual)VisualTreeHelper.GetParent(myPopup.Child)); 

hwndSource.EnableSurfaceInput();

这篇关于WPF中的浮动自定义触摸控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆