开发者

Passing Click Events in Composite WPF/XAML Controls

开发者 https://www.devze.com 2022-12-07 23:48 出处:网络
So, let\'s say I have a DataTemplate: <DataTemplate x:Key=\"ProjectsDataItemTemplate\"> <ComboBoxItem x:Name=\"ProjectComboBox\"

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 o开发者_StackOverflowverlap 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?


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

精彩评论

暂无评论...
验证码 换一张
取 消