ListBoxItem 样式中的某些事件未触发
Some event in ListBoxItem style are not firing
我有一个列表框,绑定到我的自定义对象 MyNode
的集合上,我想设置两个事件,Drop
和 MouseRightButtonDown
。列表框有自定义控件和样式。
我的 ListBoxItem
的样式如下所示:
<UserControl.Resources>
<Style TargetType="{x:Type ListBoxItem}" x:Key="pinnedListBoxStyle">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="#EEF6FC"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="#74B4E4"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<EventSetter Event="Drop" Handler="ListBoxItem_Drop"/>
<EventSetter Event="MouseRightButtonDown" Handler="ListBoxItem_MouseRightButtonDown"/>
</Style>
</UserControl.Resources>
我正在使用带有自定义控件的列表框,例如:
<ListBox SelectionMode="Single" Focusable="True" IsSynchronizedWithCurrentItem="True" AllowDrop="True"
ItemContainerStyle="{StaticResource pinnedListBoxStyle}">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:MyNode}">
<local:MyItemTemplate/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
问题:
我的两个事件处理程序之一,即 ListBoxItem_MouseRightButtonDown
,在我右键单击任何项目时从未被触发。另一个处理程序 ListBoxItem_Drop
总是按预期触发。
你有什么想法吗?
按照@Clemens 的建议,使用 PreviewMouseRightButtonDown
或 PreviewMouseRightButtonUp
都会触发我的事件。
我现在正在使用:
<Style TargetType="{x:Type ListBoxItem}" ...>
...
<EventSetter Event="PreviewMouseRightButtonDown" Handler="ListBoxItem_MouseRightButtonDown"/>
</Style>
我有一个列表框,绑定到我的自定义对象 MyNode
的集合上,我想设置两个事件,Drop
和 MouseRightButtonDown
。列表框有自定义控件和样式。
我的 ListBoxItem
的样式如下所示:
<UserControl.Resources>
<Style TargetType="{x:Type ListBoxItem}" x:Key="pinnedListBoxStyle">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="#EEF6FC"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="#74B4E4"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<EventSetter Event="Drop" Handler="ListBoxItem_Drop"/>
<EventSetter Event="MouseRightButtonDown" Handler="ListBoxItem_MouseRightButtonDown"/>
</Style>
</UserControl.Resources>
我正在使用带有自定义控件的列表框,例如:
<ListBox SelectionMode="Single" Focusable="True" IsSynchronizedWithCurrentItem="True" AllowDrop="True"
ItemContainerStyle="{StaticResource pinnedListBoxStyle}">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:MyNode}">
<local:MyItemTemplate/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
问题:
我的两个事件处理程序之一,即 ListBoxItem_MouseRightButtonDown
,在我右键单击任何项目时从未被触发。另一个处理程序 ListBoxItem_Drop
总是按预期触发。
你有什么想法吗?
按照@Clemens 的建议,使用 PreviewMouseRightButtonDown
或 PreviewMouseRightButtonUp
都会触发我的事件。
我现在正在使用:
<Style TargetType="{x:Type ListBoxItem}" ...>
...
<EventSetter Event="PreviewMouseRightButtonDown" Handler="ListBoxItem_MouseRightButtonDown"/>
</Style>