2011-10-24 38 views
0

我在MVVM場景中使用了TreeView。由於子TreeViewItem的顯示和上下文菜單取決於視圖模型的類型,我使用數據模板來選擇正確的UserControl來顯示(比StyleSelector更容易管理)。處理整個UserControl表面上的點擊事件

我的問題是,當UserControl被點擊表面的任何地方時,我需要處理命令。我使用直接附加到UserControl的EventTrigger,但只有當我點擊TextBlock或圖像的文本時,纔會處理點擊事件。這裏的示例代碼:

<UserControl x:Class="FolderTreeView" 
      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:i="http://schemas.microsoft.com/expression/2010/interactivity"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="MouseLeftButtonDown"> 
      <i:InvokeCommandAction Command="{Binding Path=DisplayCommand}"/> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
    <StackPanel Orientation="Horizontal"> 
     <Image Source="{Binding Path=Icon}"/> 
     <TextBlock Text="{Binding Path=DisplayName}"/> 
    </StackPanel> 
</UserControl> 

任何想法我可以得到這個工作?

回答

1

給你的UserControl背景顏色。默認情況下,背景顏色爲Transparent,這意味着命中測試正好穿過它

<UserControl Background="White" ... /> 
相關問題