artos
artos

Reputation: 751

How to stop the treeview collaps event after mouse click

How can I stop the treeview collaps event after mouse click on TreeViewItem arrow (on view)? I need to show my treeview expanded all time.

Upvotes: 3

Views: 2837

Answers (3)

pistipanko
pistipanko

Reputation: 775

You could 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>

Upvotes: 4

Eirik
Eirik

Reputation: 4205

You can set the Collapsed event on the TreeViewItem to this:

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

It doesn't prevent it from collapsing however, it just automatically expands it whenever it's collapsed.

Upvotes: 1

brunnerh
brunnerh

Reputation: 184386

Just retemplate the TreeViewItems to not even have an arrow (and collapsible area).

e.g.

<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 bare minimum, you will want to have triggers to show the current selection if you need that)

Upvotes: 0

Related Questions