evanb
evanb

Reputation: 3101

How to avoid the ScatterViewItem from capturing mouse up events?

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.

Upvotes: 0

Views: 793

Answers (2)

Robert Levy
Robert Levy

Reputation: 29073

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.

Upvotes: 1

Vicro
Vicro

Reputation: 575

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.

Upvotes: 0

Related Questions