2009-10-14 51 views
2

所以,讓我們說我有一個DataTemplate傳遞單擊複合WPF/XAML活動控制

<DataTemplate x:Key="ProjectsDataItemTemplate"> 
    <ComboBoxItem x:Name="ProjectComboBox" 
        Opacity="1" HorizontalAlignment="Stretch" 
        Foreground="#FF80BBD2" 
        VerticalAlignment="Center" VerticalContentAlignment="Center" 
        Background="Transparent" 
        Style="{DynamicResource ComboBoxItemStyle1}"> 
     <StackPanel> 
      <Label Content="{Binding Name}" Height="32" VerticalContentAlignment="Top" 
        FontWeight="Bold" Foreground="#FFFEF9F9" AllowDrop="True" /> 
      <TextBlock Text="{Binding Description}" 
         Foreground="#FF80BBD2" 
         Padding="5,0,0,10" 
         FontStyle="Italic" /> 
     </StackPanel> 
    </ComboBoxItem> 
</DataTemplate> 

在這種情況下,LabelTextBlock兩個重疊的ComboBoxItem可點擊區域。當我點擊其中一個子控件時,如何忽略和/或傳遞點擊到ComboBoxItem

回答

2

只需設置IsHitTestVisible屬性設置爲false這些元素:

<DataTemplate x:Key="ProjectsDataItemTemplate"> 
    <ComboBoxItem x:Name="ProjectComboBox" 
        Opacity="1" 
        HorizontalAlignment="Stretch" 
        Foreground="#FF80BBD2" 
        VerticalAlignment="Center" 
        VerticalContentAlignment="Center" 
        Background="Transparent" 
        Style="{DynamicResource ComboBoxItemStyle1}"> 
      <StackPanel> 
       <Label IsHitTestVisible="False" 
         Content="{Binding Name}" 
         Height="32" 
         VerticalContentAlignment="Top" 
         FontWeight="Bold" 
         Foreground="#FFFEF9F9" 
         AllowDrop="True" /> 
       <TextBlock IsHitTestVisible="False" 
          Text="{Binding Description}" 
          Foreground="#FF80BBD2" 
          Padding="5,0,0,10" 
          FontStyle="Italic" /> 
      </StackPanel> 
    </ComboBoxItem> 
</DataTemplate> 
+0

奇怪。我之前通過在Label和TextBlock上添加IsHitTestVisible = False來嘗試。它看起來像你必須將它添加到ComboBoxItem,這似乎有點違反直覺:我想選擇一個ComboBoxItem爲什麼我不希望它測試?無論如何,謝謝你的解決方案! – 2009-10-14 20:58:26