如何 select 列表视图中 ExpanderDoubleClick Header 上的项目

how to select item on ExpanderDoubleClick Header in Listview

我有一个带有不同 itemTemplate 的 ListView,每个 itemTemplates 里面都有一个 ExpanderDoubleClick : Expander

我想在具有自定义 selection 设计的列表视图中以扩展模式 select 项目(在每个项目 UserControl 中声明)。 所以这是我的 xaml 列表视图:

<ListView x:Name="ListViewModules" ItemsSource="{Binding ListOfModules}"
                      ItemContainerStyle="{StaticResource ContainerListViewItemStyle}"
                      ItemTemplateSelector="{DynamicResource ModuleTemplateSelector}" 
                      SelectionMode="Extended"
                      dd:DragDrop.IsDragSource="True"
                      dd:DragDrop.IsDropTarget="True"
                      dd:DragDrop.UseDefaultDragAdorner="True"
                      Grid.Column="0" Grid.Row="0" Height="494" Width="634" Background="#FFCDCDCD" 
                      ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Visible" 
                      BorderBrush="#666666" BorderThickness="1" Padding="0"
                      ClipToBounds="True" SnapsToDevicePixels="True" >                   
</ListView>

具有移除标准蓝色的样式 selection(在 App.xaml 中声明):

<Style x:Key="ContainerListViewItemStyle" TargetType="{x:Type ListViewItem}">
    <Setter Property="Margin" Value="2,2,2,0"/>
    <Setter Property="dd:DragDrop.DragSourceIgnore" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border x:Name="Bd" BorderBrush="Transparent" BorderThickness="0" Background="Transparent" Padding="0" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我每个 "UserControl's" 都有不同的 ItemTemplates,这里是其中一个示例 selection 设计:

<UserControl x:Class="Topinambour.Templates.Modules.DelayTemplate" ...>
<Grid Width="602" MinHeight="24">
    <Grid.ColumnDefinitions>            <ColumnDefinition Width="40"/><ColumnDefinition />        </Grid.ColumnDefinitions>
    <ToggleButton x:Name="TgBtIsActive" IsChecked="{Binding IsChecked}" IsThreeState="{Binding IsThreeState}" Grid.Column="0" Height="24" Width="40" click="TgBtIsActive_Click"/>
    <templates:ExpanderDoubleClick x:Name="ModuleExpander" IsExpanded="{Binding IsExpanded, Mode=OneWay}" Height="auto" Template="{DynamicResource TemplateExpander}" Grid.Column="1" Margin="4,0,0,0" Collapsed="Expander_Collapsed" Expanded="Expander_Expanded" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
        <templates:ExpanderDoubleClick.Resources>
            <Style x:Key="ExpanderHeaderStyle" TargetType="{x:Type ToggleButton}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ToggleButton}">
                            <Border x:Name="BorderIndent" BorderBrush="Gray" BorderThickness="1" Height="24" Width="558" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Padding="{Binding Indent}">
                            ... my  Header Content ...
                            </Border>
                            <ControlTemplate.Triggers>
                                <DataTrigger Binding="{Binding IsExpanded}" Value="true">
                                    <Setter Property="IsEnabled" TargetName="TbComment" Value="true"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListViewItem}}" Value="true">
                                    <Setter Property="Background" TargetName="headerCanvas" Value="#999999"/>
                                    <Setter Property="BorderBrush" TargetName="TbComment" Value="#666666"/>
                                    <Setter Property="Foreground" TargetName="LbHeaderTitle" Value="White"/>
                                </DataTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </templates:ExpanderDoubleClick.Resources>
        <Border Background="White" BorderBrush="#666666" BorderThickness="1,0,1,1" Height="136" Width="558">
            <Grid>
                ... my Expanded Content ...
            </Grid>
        </Border>
    </templates:ExpanderDoubleClick>
</Grid>

这里是 ExpanderDoubleClick.cs:

public class ExpanderDoubleClick: Expander
{
    private static readonly DependencyPropertyKey IsMouseDoubleClickedPropertyKey = DependencyProperty.RegisterReadOnly(
        "IsMouseDoubleClicked",typeof(Boolean),typeof(ExpanderDoubleClick),new FrameworkPropertyMetadata(false));
    public static readonly DependencyProperty IsMouseDoubleClickedProperty = IsMouseDoubleClickedPropertyKey.DependencyProperty;
    public Boolean IsMouseDoubleClicked
    {
        get { return (Boolean)GetValue(IsMouseDoubleClickedProperty); }
    }
    static ExpanderDoubleClick()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ExpanderDoubleClick), new FrameworkPropertyMetadata(typeof(ExpanderDoubleClick)));
    }
    protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
    {
        base.OnMouseDoubleClick(e);
    }
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        ContentControl contentControl = base.GetTemplateChild("HeaderSite") as ContentControl;
        if (contentControl != null)
        {
            contentControl.AddHandler(ContentControl.MouseDoubleClickEvent, new MouseButtonEventHandler(ExpanderHeader_MouseDoubleClick), true);
        }
    }
    private void ExpanderHeader_MouseDoubleClick(Object sender, MouseButtonEventArgs e)
    {
        base.SetValue(IsMouseDoubleClickedPropertyKey, !IsMouseDoubleClicked);
        base.IsExpanded= !base.IsExpanded;
    }
}

当我单击扩展器的 header 时,selection 不起作用,但如果我打开它并在 expandedContent 内部单击,它 select 项目!我错过了什么 ?谢谢你的回复。

我终于找到了一个很好的解决方案,可以保持我的扩展模式选择。

因为 header 接缝无法接收要处理的 Click 事件。 我查看了树调试,它显示点击 header 没有经过 "ListViewItem"

所以我在边框 x.Name="BorderIdent" 上添加了一个 OnMouseLeftButtonDown 事件,然后将 RaisedEvent 添加回它的 itemTemplate 像这样:

private void BorderIndent_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    ListViewItem lvItem = UITools.FindAncestor<ListViewItem>(e.OriginalSource as DependencyObject);
    bool isListViewItem = lvItem != null;
    if (isListViewItem)
    {
        lvItem.RaiseEvent(e);
    }
}

UITools.FindAncestor : 是 here

并且一切都完美:)