简体   繁体   English

如何在后面的代码中将TreeViewItem的IsExpanded属性更新为false?

[英]How to Update TreeViewItem's IsExpanded property to false in code behind?

I am working with WPF TreeView control. 我正在使用WPF TreeView控件。 I am creating a hierarchical data structure and assigning it to ItemsSource and it will generate TreeviewItems automatically. 我正在创建一个分层数据结构并将其分配给ItemsSource,它将自动生成TreeviewItems。 By default I use IsExpanded of TreeViewItem to true. 默认情况下,我将TreeViewItem的IsExpanded设置为true。 But in a particular case, I want to set IsExpanded property to false. 但是在特定情况下,我想将IsExpanded属性设置为false。 So that treeview loading doesn't take time to generate all items. 因此,树视图加载无需花费时间即可生成所有项目。 How can I set that in code since I don't have reference to TreeViewItem's instance at that time? 由于当时我没有引用TreeViewItem的实例,该如何在代码中进行设置?

Edit: 编辑:

I am looking for a way so that I can set all TreeViewItem's default behaviour in my TreeView to collapsed while doing a specific operation and set back to Expanded when this operation completes. 我正在寻找一种方法,可以在执行特定操作时将TreeView中所有TreeViewItem的默认行为设置为折叠,并在此操作完成时将其设置回Expanded。

IsExpanded defaults to false, so I assume you have a Style changing the default to true. IsExpanded默认为false,因此我假设您有一个Style将默认值更改为true。 If you change this Style to use a Binding (and change the value during your "specific operation") then the TreeViewItems without an explicitly set IsExpanded will default to false instead: 如果您更改此Style以使用Binding(并在“特定操作”期间更改值),则没有显式设置IsExpanded的TreeViewItems将默认为false:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <CheckBox x:Name="chkDefaultExpanded" Content="Default Expanded"/>
        <TreeView>
            <TreeView.Resources>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="{Binding ElementName=chkDefaultExpanded, Path=IsChecked}"/>
                </Style>
            </TreeView.Resources>
            <TreeViewItem Header="Do">
                <TreeViewItem Header="A">
                    <TreeViewItem Header="1"/>
                    <TreeViewItem Header="2"/>
                    <TreeViewItem Header="3"/>
                </TreeViewItem>
                <TreeViewItem Header="B"/>
                <TreeViewItem Header="C"/>
            </TreeViewItem>
            <TreeViewItem Header="Re">
                <TreeViewItem Header="D">
                    <TreeViewItem Header="4"/>
                    <TreeViewItem Header="5"/>
                    <TreeViewItem Header="6"/>
                </TreeViewItem>
                <TreeViewItem Header="E"/>
                <TreeViewItem Header="F"/>
            </TreeViewItem>
        </TreeView>
    </StackPanel>
</Grid>

A way to do this is to use a ViewModel, ie an abstraction of the UI, based on the model (the data). 一种方法是使用ViewModel,即基于模型(数据)的UI的抽象。 If you include a bool property (eg IsExpanded) in the part of the ViewModel related to the tree data, you can bind the TreeViewItem's IsExpanded property to the IsExpanded property of the ViewModel. 如果在与树数据相关的ViewModel的一部分中包含bool属性(例如IsExpanded),则可以将TreeViewItem的IsExpanded属性绑定到ViewModel的IsExpanded属性。 The view is bound to the ViewModel which includes a copy or a reference of the Model. 该视图绑定到ViewModel,后者包含模型的副本或引用。

Then, expanding or collapsing parts of the tree gets to be as simple as updating the ViewModel (which needs to implement INotifyPropertyChanged or define Dependency properties). 然后,扩展或折叠树的一部分就变得与更新ViewModel一样简单(需要实现INotifyPropertyChanged或定义Dependency属性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM