如何在单击项目时展开WPF TreeView

AKo*_*ran 13 wpf treeview

现在你必须双击或点击+图标.如果用户点击它扩展的节点上的任何地方,有没有办法实现它?

BJe*_*ngs 16

我有同样的问题,并通过另一个StackOverflow帖子找到了一个很好的解决方案.

在control.xaml的TreeView元素中,您可以直接挂钩到TreeViewItem的Selected事件:

<TreeView ItemsSource="{StaticResource Array}" TreeViewItem.Selected="TreeViewItem_Selected"/>
Run Code Online (Sandbox Code Playgroud)

然后在您的control.xaml.cs代码中,您可以从RoutedEventArgs中获取所选的TreeViewItem并将其设置为IsExpanded:

private void TreeViewItem_Selected(object sender, RoutedEventArgs e)
{
    TreeViewItem tvi = e.OriginalSource as TreeViewItem;

    if (tvi == null || e.Handled) return;

    tvi.IsExpanded = !tvi.IsExpanded;
    e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)

干净整洁.希望这有助于某人!

  • 在我看来,这是最好的方法!只有一个缺陷:当您尝试通过单击折叠图标来折叠未选择的项目时,它会被选中,事件处理程序会阻止它折叠.你必须在`if(!e.Handled){...}`中封装事件处理程序,并在切换`IsExpanded`属性后立即设置`e.Handled = true;`. (2认同)

Mar*_*ote 5

也许不是最优雅的解决方案,但这有效:

    static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
    {
        while (source != null && source.GetType() != typeof(T))
            source = VisualTreeHelper.GetParent(source);

        return source;
    }
Run Code Online (Sandbox Code Playgroud)

然后在TreeViewItem.Selected处理程序中:

        private void Treeview_Selected(object sender, RoutedEventArgs e)
        {
            var treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
            if (treeViewItem != null) treeViewItem.IsExpanded = true;
        }
Run Code Online (Sandbox Code Playgroud)

从这里获取VisualUpwardSearch魔术:在显示ContextMenu之前,右键单击选择TreeView Node

问候


Dan*_*iel 5

我遇到了同样的问题,我使用样式功能做到了这一点,这样您就不需要处理该事件。

我为 TreeViewItem 定义了一个样式

<Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
    <!--<Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>-->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TreeViewItem}">
                        <CheckBox Style="{StaticResource TreeViewItemCB}" IsChecked="{Binding Path=IsExpanded,Mode=OneWayToSource,RelativeSource={RelativeSource TemplatedParent}}"  ClickMode="Press">
                            <Grid Background="{StaticResource TreeViewItemBackground}" Margin="0">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions> 
                            <Border Name="Bd">
                                    <ContentPresenter x:Name="PART_Header" ContentSource="Header"/>
                            </Border>
                            <ItemsPresenter x:Name="ItemsHost" Grid.Row="1"/>
                            </Grid>
                        </CheckBox>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

重要的部分是在 ControlTemplate 中定义复选框以及与其绑定。当复选框被选中时,只需单击一下即可展开该项目。

<CheckBox Style="{StaticResource TreeViewItemCB}" IsChecked="{Binding Path=IsExpanded,Mode=OneWayToSource,RelativeSource={RelativeSource TemplatedParent}}"  ClickMode="Press">
Run Code Online (Sandbox Code Playgroud)

这是复选框的样式,以便它拉伸并且不显示带有笔划的框。

<Style x:Key="TreeViewItemCB" TargetType="CheckBox" BasedOn="{StaticResource baseStyle}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="VerticalAlignment" Value="Stretch"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="CheckBox">
                <ContentPresenter VerticalAlignment="Stretch" HorizontalAlignment="Stretch" RecognizesAccessKey="True"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)