How to stop the treeview collaps event after mouse click

三世轮回 提交于 2021-01-27 12:36:01

问题


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.


回答1:


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>



回答2:


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.




回答3:


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)



来源:https://stackoverflow.com/questions/10517837/how-to-stop-the-treeview-collaps-event-after-mouse-click

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!