在复合WPF/XAML控件中传递单击事件
假设我有一个在复合WPF/XAML控件中传递单击事件,wpf,xaml,mouseevent,Wpf,Xaml,Mouseevent,假设我有一个数据模板: <DataTemplate x:Key="ProjectsDataItemTemplate"> <ComboBoxItem x:Name="ProjectComboBox" Opacity="1" HorizontalAlignment="Stretch" Foreground="#FF80BBD2" VerticalAlignmen
数据模板
:
<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>
在这种情况下,标签
和文本块
都与组合框项目
的可点击区域重叠。当我单击它的一个子控件时,如何忽略和/或传递对ComboBoxItem的点击?只需将这些元素的属性设置为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>
奇怪。我之前尝试过在标签和TextBlock上添加IshitteVisible=False。看起来您也必须将其添加到ComboBoxItem中,这似乎有点违反直觉:我想选择ComboxItem为什么不希望它经过命中测试?无论如何,谢谢你的解决方案!