如何避免 ScatterViewItem 捕获鼠标按下事件?

发布于 2024-11-03 04:25:28 字数 1988 浏览 2 评论 0原文

基本上我有一个 Surface ToolKit ScatterView 绑定到模板化为 ImageView 的图像路径列表。 ImageView 控件使用 MouseUp 事件,但该事件不会在鼠标松开时触发。我也用 PreviewMouseUp 尝试过,但没有成功。澄清一下,我正在使用 Surface ToolKit 构建 Win7 触摸应用程序。

窗口.xaml:

<s:SurfaceWindow x:Class="SurfaceApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://schemas.microsoft.com/surface/2008"
xmlns:view="clr-namespace:SurfaceApplication1"
Title="SurfaceApplication1"
>
<Grid>
    <s:ScatterView x:Name="scatterView">
        <s:ScatterView.ItemTemplate>
            <DataTemplate>
                <view:ImageView DataContext="{Binding}" MinHeight="300" MinWidth="300"/>
            </DataTemplate>
        </s:ScatterView.ItemTemplate>
    </s:ScatterView>
</Grid>

Window.xaml.cs:

public Window1()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(SurfaceWindow1_Loaded);
    }

    void SurfaceWindow1_Loaded(object sender, RoutedEventArgs e)
    {
        scatterView.ItemsSource = Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg");
    }

ImageView.xaml:

<UserControl x:Class="SurfaceApplication1.ImageView"
         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" PreviewMouseUp="Grid_MouseUp">
<Grid MouseUp="Grid_MouseUp">
    <StackPanel>
        <Image Source="{Binding}"/>
        <TextBlock Text="{Binding}" Height="20" Width="100"/>
    </StackPanel>
</Grid>

有谁知道解决这个问题的方法吗?我需要在 ImageView 中处理 MouseUp 事件。谢谢。

Basically I have a Surface ToolKit ScatterView bounded to a list of image paths which are templated as an ImageView. The ImageView control uses the MouseUp events but that event is not fired on mouse up. I have tried it with PreviewMouseUp as well and no luck. Just to clarify, I am building a Win7 touch app using the Surface ToolKit.

Window.xaml:

<s:SurfaceWindow x:Class="SurfaceApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://schemas.microsoft.com/surface/2008"
xmlns:view="clr-namespace:SurfaceApplication1"
Title="SurfaceApplication1"
>
<Grid>
    <s:ScatterView x:Name="scatterView">
        <s:ScatterView.ItemTemplate>
            <DataTemplate>
                <view:ImageView DataContext="{Binding}" MinHeight="300" MinWidth="300"/>
            </DataTemplate>
        </s:ScatterView.ItemTemplate>
    </s:ScatterView>
</Grid>

Window.xaml.cs:

public Window1()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(SurfaceWindow1_Loaded);
    }

    void SurfaceWindow1_Loaded(object sender, RoutedEventArgs e)
    {
        scatterView.ItemsSource = Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg");
    }

ImageView.xaml:

<UserControl x:Class="SurfaceApplication1.ImageView"
         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" PreviewMouseUp="Grid_MouseUp">
<Grid MouseUp="Grid_MouseUp">
    <StackPanel>
        <Image Source="{Binding}"/>
        <TextBlock Text="{Binding}" Height="20" Width="100"/>
    </StackPanel>
</Grid>

Does anyone know of a way around this? I need the MouseUp event to be handled in the ImageView. THanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女山的月亮 2024-11-10 04:25:29

鼠标被捕获到 ScatterViewItem,这意味着鼠标事件不会到达该项目的子项。您需要将此事件处理程序放置在 ScatterViewItem 本身或该项目的父项上。在用户控件的代码中,您可能可以按照 Parent.MouseUpEvent += yourHandler 的方式执行某些操作,

但我建议在 ScatterViewItem 上使用 ContainerActivated 和 ContainerDeactivated 事件,而不是 mouseup/down 。当第一次触摸按下和最后一次触摸出现时,将发生激活事件。在多点触控世界中,您必须仔细考虑当用户在任何控件上使用多个手指并一次抬起这些手指时会发生什么。

The mouse is captured to the ScatterViewItem which means that mouse events won't reach children of the item. You'll need to place this event handler on the ScatterViewItem itself or a parent of the item. In the usercontrol's code you could probably do something along the lines of Parent.MouseUpEvent += yourHandler

I'd recommend using the ContainerActivated and ContainerDeactivated events on ScatterViewItem instead of mouseup/down though. The activation events will happen when the first touch goes down and when the last touch comes up. In the multitouch world you have to think carefully about what will happen when the user uses multiple fingers on any control and lifts those fingers one at a time.

古镇旧梦 2024-11-10 04:25:29

而不是使用正常的事件语法。您可以在代码隐藏中使用 UIElement.AddHandler 方法,并指定您想要接收已处理事件。

MyGrid.AddHandler(Button.ClickEvent, new RoutedEventHandler(GetHandledToo), true);

您可以在 MSDN。

Instead of using the normal event syntax. You can use the UIElement.AddHandler method in code behind and specify that you want to receive handled events

MyGrid.AddHandler(Button.ClickEvent, new RoutedEventHandler(GetHandledToo), true);

You can read about it on MSDN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文