2011-04-20 23 views
0

基本上我有一個表面工具包ScatterView綁定到模板化爲ImageView的圖像路徑列表。 ImageView控件使用MouseUp事件,但該事件不會在鼠標上觸發。我也嘗試過PreviewMouseUp,沒有運氣。爲了澄清,我正在使用Surface ToolKit構建一個Win7觸摸應用程序。如何避免ScatterViewItem捕獲鼠標事件?

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事件。謝謝。

回答

1

讀到它的鼠標被捕獲,這意味着ScatterViewItem鼠標事件不會觸及該項目的孩子。您需要將此事件處理程序放在ScatterViewItem本身或項目的父項上。在用戶控件的代碼中,你可能會沿着Parent.MouseUpEvent += yourHandler

的行執行某些操作我建議在ScatterViewItem上使用ContainerActivated和ContainerDeactivated事件,而不是使用mouseup/down。激活事件將在第一次觸摸關閉和最後一次觸摸出現時發生。在多點觸控世界中,您必須仔細考慮用戶在任何控制器上使用多根手指並每次擡起一根手指時會發生什麼。

0

而不是使用正常的事件語法。你可以在後面的代碼使用UIElement.AddHandler方法,並指定要接收處理事件

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

您可以在MSDN.

+0

不幸的是,這也不起作用。 – evanb 2011-04-20 23:57:44