如何避免ScatterViewItem捕获鼠标向上事件
本文关键字:事件 鼠标 何避免 ScatterViewItem | 更新日期: 2023-09-27 17:50:38
基本上,我有一个Surface ToolKit ScatterView绑定到图像路径列表,这些路径被模板化为ImageView。ImageView控件使用MouseUp事件,但该事件不会在鼠标向上时触发。我也用PreviewMouseUp试过,没有运气。为了澄清,我正在使用Surface ToolKit构建一个Win7触摸应用程序。
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>
有谁知道解决这个问题的方法吗?我需要在ImageView中处理MouseUp事件。谢谢。
鼠标被捕获到ScatterViewItem,这意味着鼠标事件不会到达该项目的子元素。您需要将此事件处理程序放置在ScatterViewItem本身或该项的父项上。在usercontrol的代码中,你可能会做一些Parent.MouseUpEvent += yourHandler
我建议在ScatterViewItem上使用ContainerActivated和ContainerDeactivated事件,而不是鼠标up/down。激活事件将在第一次触摸下降和最后一次触摸上升时发生。在多点触控的世界里,你必须仔细考虑当用户在任何控件上使用多个手指并且每次抬起一个手指时将会发生什么。
不使用正常的事件语法。您可以使用UIElement。方法,并指定您希望接收已处理的事件
MyGrid.AddHandler(Button.ClickEvent, new RoutedEventHandler(GetHandledToo), true);
你可以在MSDN上阅读