How to stop a treeview collapse event after a mouse click

How can I stop the collaps treeview event after clicking on the TreeViewItem arrow (in the view)? I need to show that my tree has been expanded all the time.

+3
source share
3 answers

You can set the Collapsed event in XAML:

<TreeView 
    Name="myTreeView"
    ItemsSource="{Binding dataSource}">
            <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="True" />
                    <Style.Triggers>
                        <EventTrigger RoutedEvent="Collapsed">
                            <EventTrigger.Actions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <BooleanAnimationUsingKeyFrames 
                                            Duration="0" 
                                            Storyboard.TargetProperty="(TreeViewItem.IsExpanded)">
                                            <DiscreteBooleanKeyFrame KeyTime="0" Value="True" />
                                        </BooleanAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                    </Style.Triggers>
                </Style>                            
            </TreeView.ItemContainerStyle>
        </TreeView>
+4
source

You can set the Collapsed event on TreeViewItem as follows:

private void TreeViewItem_Collapsed(object sender, RoutedEventArgs e)
{
    (sender as TreeViewItem).IsExpanded = true;
}

However, this does not stop him from folding, he simply automatically expands it whenever it collapses.

+1
source

Just a retemplate TreeViewItems so you don’t even have an arrow (and a folding area).

eg.

<Style TargetType="TreeViewItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TreeViewItem">
                <StackPanel>
                    <ContentPresenter ContentSource="Header"/>
                    <ItemsPresenter Margin="20,0,0,0"/>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

(This is the minimum minimum, you want triggers to display the current selection if you need it)

0
source

All Articles