ListBoxItem样式中的某些事件未触发

时间:2018-06-06 06:34:48

标签: wpf xaml listviewitem

我有一个列表框,绑定在我的自定义对象MyNode的集合上,我想设置两个事件DropMouseRightButtonDown。列表框具有自定义控件和样式。

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总是按预期触发。

你能有什么想法吗?

1 个答案:

答案 0 :(得分:0)

根据@ Clemens的建议,使用PreviewMouseRightButtonDownPreviewMouseRightButtonUp会触发我的活动。

我现在正在使用:

<Style TargetType="{x:Type ListBoxItem}" ...>
    ...
    <EventSetter Event="PreviewMouseRightButtonDown" Handler="ListBoxItem_MouseRightButtonDown"/>
</Style>