Tyler Schlegel
Tyler Schlegel

Reputation: 23

Passing Click Events in Composite WPF/XAML Controls

So, let's say I have a DataTemplate:

<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>

In this case, the Label and the TextBlock both overlap the clickable area for the ComboBoxItem. How do I ignore and/or pass a click through to the ComboBoxItem when I click one of its child controls?

Upvotes: 2

Views: 926

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564831

Just set the IsHitTestVisible property to false for those elements:

<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>

Upvotes: 2

Related Questions