C# 如何在启动或筛选后始终展开treeview中的前2个节点

C# 如何在启动或筛选后始终展开treeview中的前2个节点,c#,wpf,mvvm,treeview,C#,Wpf,Mvvm,Treeview,我有一个带有mvvm和集合的示例项目来构建树状视图 我想知道如何实现总是展开前2个节点时启动或过滤后的树视图 XAML 视图模型 public class MainWindowViewModel : PropertyChangedBase { private bool _isDisplayGreen = true; private bool _isDisplayRed = true; private bool _isDisplayYellow = true;

我有一个带有mvvm和集合的示例项目来构建树状视图

我想知道如何实现总是展开前2个节点时启动或过滤后的树视图

XAML


视图模型

public class MainWindowViewModel : PropertyChangedBase
{
    private bool _isDisplayGreen = true;
    private bool _isDisplayRed = true;
    private bool _isDisplayYellow = true;

    public bool IsDisplayGreen
    {
        get { return _isDisplayGreen; }
        set
        {
            _isDisplayGreen = value;
            FilterFunction();
            OnPropertyChanged("IsDisplayRed");
            OnPropertyChanged("IsDisplayGreen");
            OnPropertyChanged("IsDisplayYellow");
        }
    }

    public bool IsDisplayRed
    {
        get { return _isDisplayRed; }
        set
        {
            _isDisplayRed = value;
            FilterFunction();
            OnPropertyChanged("IsDisplayRed");
            OnPropertyChanged("IsDisplayGreen");
            OnPropertyChanged("IsDisplayYellow");
        }
    }

    public bool IsDisplayYellow
    {
        get { return _isDisplayYellow; }
        set
        {
            if (value == false)
            {
                _isDisplayGreen = value;
            }

            _isDisplayYellow = value;
            FilterFunction();
            OnPropertyChanged("IsDisplayRed");
            OnPropertyChanged("IsDisplayGreen");
            OnPropertyChanged("IsDisplayYellow");
        }
    }

    private void FilterFunction()
    {
        ICollectionView classesDataSourceView = CollectionViewSource.GetDefaultView(ClassList);

        classesDataSourceView.Filter = (classModel =>
        {
            ICollectionView studentsDataSourceView = CollectionViewSource.GetDefaultView(((Class)classModel).Students);

            if (_isDisplayGreen == false && _isDisplayYellow == false && _isDisplayRed == false)
            {
                studentsDataSourceView.Filter = (studentModel => false);
            }

            if (_isDisplayGreen == true && _isDisplayYellow == true && _isDisplayRed == true)
            {
                studentsDataSourceView.Filter = (studentModel => true);
            }

            if (_isDisplayGreen == false && _isDisplayYellow == true && _isDisplayRed == false)
            {
                studentsDataSourceView.Filter = (studentModel => ((Student)studentModel).ImagePath.Equals("../Images/Yellow-icon.png"));
            }

            if (_isDisplayGreen == true && _isDisplayYellow == false && _isDisplayRed == false)
            {
                studentsDataSourceView.Filter = (studentModel => ((Student)studentModel).ImagePath.Equals("../Images/Green-icon.png"));
            }

            if (_isDisplayGreen == false && _isDisplayYellow == false && _isDisplayRed == true)
            {
                studentsDataSourceView.Filter = (studentModel => ((Student)studentModel).ImagePath.Equals("../Images/Red-icon.png"));
            }

            if (_isDisplayGreen == true && _isDisplayYellow == false && _isDisplayRed == true)
            {
                studentsDataSourceView.Filter = (studentModel => ((Student)studentModel).ImagePath.Equals("../Images/Red-icon.png") || ((Student)studentModel).ImagePath.Equals("../Images/Green-icon.png"));
            }

            if (_isDisplayGreen == false && _isDisplayYellow == true && _isDisplayRed == true)
            {
                studentsDataSourceView.Filter = (studentModel => ((Student)studentModel).ImagePath.Equals("../Images/Red-icon.png") || ((Student)studentModel).ImagePath.Equals("../Images/Yellow-icon.png"));
            }

            if (_isDisplayGreen == true && _isDisplayYellow == true && _isDisplayRed == false)
            {
                studentsDataSourceView.Filter = (studentModel => ((Student)studentModel).ImagePath.Equals("../Images/Yellow-icon.png") || ((Student)studentModel).ImagePath.Equals("../Images/Green-icon.png"));
            }

            return !studentsDataSourceView.IsEmpty;
        });
    }

    private ObservableCollection<Class> classList;

    public ObservableCollection<Class> ClassList
    {
        get { return classList; }
        set { classList = value; }
    }

    public MainWindowViewModel()
    {
        classList = GetAll();
    }

    public ObservableCollection<Class> GetAll()
    {
        ObservableCollection<Class> classList = new ObservableCollection<Class>();
        Class treeItem = null;
        string greenIconPath = "../Images/Green-icon.png";
        string redIconPath = "../Images/Red-icon.png";
        string yellowIconPath = "../Images/Yellow-icon.png";

        treeItem = new Class("Class A", redIconPath);
        treeItem.Students.Add(new Student("Student 1", greenIconPath));
        treeItem.Students.Add(new Student("Student 2", greenIconPath));
        treeItem.Students.Add(new Student("Student 3", redIconPath));
        classList.Add(treeItem);

        treeItem = new Class("Class B", yellowIconPath);
        treeItem.Students.Add(new Student("Student 1", yellowIconPath));
        treeItem.Students.Add(new Student("Student 2", greenIconPath));
        treeItem.Students.Add(new Student("Student 3", greenIconPath));
        classList.Add(treeItem);

        treeItem = new Class("Class C", greenIconPath);
        treeItem.Students.Add(new Student("Student 1", greenIconPath));
        treeItem.Students.Add(new Student("Student 2", greenIconPath));
        treeItem.Students.Add(new Student("Student 3", greenIconPath));
        classList.Add(treeItem);

        return classList;
    }
}
公共类MainWindowViewModel:PropertyChangedBase { 私有bool_isDisplayGreen=true; 私有bool_isdisplayed=true; 私有bool_isdisplayellow=真; 公共图书馆显示绿色 { 获取{return\u isDisplayGreen;} 设置 { _isDisplayGreen=值; FilterFunction(); 不动产变更(“显示”); OnPropertyChanged(“IsDisplayGreen”); OnPropertyChanged(“IsDisplayYellow”); } } 显示公共布尔值 { 获取{return\u isdisplayed;} 设置 { _isDisplayRed=值; FilterFunction(); 不动产变更(“显示”); OnPropertyChanged(“IsDisplayGreen”); OnPropertyChanged(“IsDisplayYellow”); } } 公共广播显示黄色 { 获取{return\u isdisplayellow;} 设置 { 如果(值==false) { _isDisplayGreen=值; } _isDisplayYellow=数值; FilterFunction(); 不动产变更(“显示”); OnPropertyChanged(“IsDisplayGreen”); OnPropertyChanged(“IsDisplayYellow”); } } 私有void FilterFunction() { ICollectionView类DataSourceView=CollectionViewSource.GetDefaultView(类列表); classesDataSourceView.Filter=(classModel=> { ICollectionView学生SDataSourceView=CollectionViewSource.GetDefaultView(((类)类模型).Students); 如果(_isDisplayGreen==false&&u isDisplayYellow==false&&u isDisplayRed==false) { studentsDataSourceView.Filter=(studentModel=>false); } 如果(_isDisplayGreen==true&&&u isDisplayYellow==true&&u isDisplayRed==true) { studentsDataSourceView.Filter=(studentModel=>true); } 如果(_isDisplayGreen==false&&u isDisplayYellow==true&&u isDisplayRed==false) { studentsDataSourceView.Filter=(studentModel=>((Student)studentModel.ImagePath.Equals(“../Images/Yellow icon.png”); } 如果(_isDisplayGreen==true&&u isDisplayYellow==false&&u isDisplayRed==false) { studentsDataSourceView.Filter=(studentModel=>((Student)studentModel.ImagePath.Equals(“../Images/Green-icon.png”); } 如果(_isDisplayGreen==false&&u isDisplayYellow==false&&u isDisplayRed==true) { studentsDataSourceView.Filter=(studentModel=>((Student)studentModel.ImagePath.Equals(“../Images/Red icon.png”); } 如果(_isDisplayGreen==true&&&u isDisplayYellow==false&&u isDisplayRed==true) { studentsDataSourceView.Filter=(studentModel=>((学生)studentModel.ImagePath.Equals(“../Images/Red-icon.png”)| |((学生)studentModel.ImagePath.Equals(“../Images/Green-icon.png”); } 如果(_isDisplayGreen==false&&u isDisplayYellow==true&&u isDisplayRed==true) { studentsDataSourceView.Filter=(studentModel=>((学生)studentModel.ImagePath.Equals(“../Images/Red icon.png”)| |((学生)studentModel.ImagePath.Equals(“../Images/Yellow icon.png”); } 如果(_isDisplayGreen==true&&&u isDisplayYellow==true&&u isDisplayRed==false) { studentsDataSourceView.Filter=(studentModel=>((学生)studentModel.ImagePath.Equals(“../Images/Yellow icon.png”)| |((学生)studentModel.ImagePath.Equals(“../Images/Green icon.png”); } return!studentsDataSourceView.IsEmpty; }); } 私有可观测集合类列表; 公共ObservableCollection类列表 { 获取{return classList;} 设置{classList=value;} } 公共主窗口视图模型() { classList=GetAll(); } 公共可观测集合GetAll() { ObservableCollection类列表=新的ObservableCollection(); 类treeItem=null; 字符串greenIconPath=“../Images/Green-icon.png”; 字符串redIconPath=“../Images/Red icon.png”; 字符串yellowIconPath=“../Images/Yellow icon.png”; treeItem=新类(“A类”,redIconPath); 添加(新学生(“学生1”,绿色图标)); 添加(新学生(“学生2”,绿色图标)); 添加(新学生(“学生3”,redIconPath)); classList.Add(treeItem); treeItem=新类别(“B类”,黄色图标); 添加(新学生(“学生1”,黄色图标)); 添加(新学生(“学生2”,绿色图标)); 添加(新学生(“学生3”,绿色图标)); classList.Add(treeItem); treeItem=新类别(“C类”,绿色通道); 添加(新学生(“学生1”,绿色图标)); 添加(新学生(“学生2”,绿色图标)); 添加(新学生(“学生3”,绿色图标)); classList.Add(treeItem); 返回类列表; } }
您可以通过绑定来完成此操作。使用树视图,它可以
public class MainWindowViewModel : PropertyChangedBase
{
    private bool _isDisplayGreen = true;
    private bool _isDisplayRed = true;
    private bool _isDisplayYellow = true;

    public bool IsDisplayGreen
    {
        get { return _isDisplayGreen; }
        set
        {
            _isDisplayGreen = value;
            FilterFunction();
            OnPropertyChanged("IsDisplayRed");
            OnPropertyChanged("IsDisplayGreen");
            OnPropertyChanged("IsDisplayYellow");
        }
    }

    public bool IsDisplayRed
    {
        get { return _isDisplayRed; }
        set
        {
            _isDisplayRed = value;
            FilterFunction();
            OnPropertyChanged("IsDisplayRed");
            OnPropertyChanged("IsDisplayGreen");
            OnPropertyChanged("IsDisplayYellow");
        }
    }

    public bool IsDisplayYellow
    {
        get { return _isDisplayYellow; }
        set
        {
            if (value == false)
            {
                _isDisplayGreen = value;
            }

            _isDisplayYellow = value;
            FilterFunction();
            OnPropertyChanged("IsDisplayRed");
            OnPropertyChanged("IsDisplayGreen");
            OnPropertyChanged("IsDisplayYellow");
        }
    }

    private void FilterFunction()
    {
        ICollectionView classesDataSourceView = CollectionViewSource.GetDefaultView(ClassList);

        classesDataSourceView.Filter = (classModel =>
        {
            ICollectionView studentsDataSourceView = CollectionViewSource.GetDefaultView(((Class)classModel).Students);

            if (_isDisplayGreen == false && _isDisplayYellow == false && _isDisplayRed == false)
            {
                studentsDataSourceView.Filter = (studentModel => false);
            }

            if (_isDisplayGreen == true && _isDisplayYellow == true && _isDisplayRed == true)
            {
                studentsDataSourceView.Filter = (studentModel => true);
            }

            if (_isDisplayGreen == false && _isDisplayYellow == true && _isDisplayRed == false)
            {
                studentsDataSourceView.Filter = (studentModel => ((Student)studentModel).ImagePath.Equals("../Images/Yellow-icon.png"));
            }

            if (_isDisplayGreen == true && _isDisplayYellow == false && _isDisplayRed == false)
            {
                studentsDataSourceView.Filter = (studentModel => ((Student)studentModel).ImagePath.Equals("../Images/Green-icon.png"));
            }

            if (_isDisplayGreen == false && _isDisplayYellow == false && _isDisplayRed == true)
            {
                studentsDataSourceView.Filter = (studentModel => ((Student)studentModel).ImagePath.Equals("../Images/Red-icon.png"));
            }

            if (_isDisplayGreen == true && _isDisplayYellow == false && _isDisplayRed == true)
            {
                studentsDataSourceView.Filter = (studentModel => ((Student)studentModel).ImagePath.Equals("../Images/Red-icon.png") || ((Student)studentModel).ImagePath.Equals("../Images/Green-icon.png"));
            }

            if (_isDisplayGreen == false && _isDisplayYellow == true && _isDisplayRed == true)
            {
                studentsDataSourceView.Filter = (studentModel => ((Student)studentModel).ImagePath.Equals("../Images/Red-icon.png") || ((Student)studentModel).ImagePath.Equals("../Images/Yellow-icon.png"));
            }

            if (_isDisplayGreen == true && _isDisplayYellow == true && _isDisplayRed == false)
            {
                studentsDataSourceView.Filter = (studentModel => ((Student)studentModel).ImagePath.Equals("../Images/Yellow-icon.png") || ((Student)studentModel).ImagePath.Equals("../Images/Green-icon.png"));
            }

            return !studentsDataSourceView.IsEmpty;
        });
    }

    private ObservableCollection<Class> classList;

    public ObservableCollection<Class> ClassList
    {
        get { return classList; }
        set { classList = value; }
    }

    public MainWindowViewModel()
    {
        classList = GetAll();
    }

    public ObservableCollection<Class> GetAll()
    {
        ObservableCollection<Class> classList = new ObservableCollection<Class>();
        Class treeItem = null;
        string greenIconPath = "../Images/Green-icon.png";
        string redIconPath = "../Images/Red-icon.png";
        string yellowIconPath = "../Images/Yellow-icon.png";

        treeItem = new Class("Class A", redIconPath);
        treeItem.Students.Add(new Student("Student 1", greenIconPath));
        treeItem.Students.Add(new Student("Student 2", greenIconPath));
        treeItem.Students.Add(new Student("Student 3", redIconPath));
        classList.Add(treeItem);

        treeItem = new Class("Class B", yellowIconPath);
        treeItem.Students.Add(new Student("Student 1", yellowIconPath));
        treeItem.Students.Add(new Student("Student 2", greenIconPath));
        treeItem.Students.Add(new Student("Student 3", greenIconPath));
        classList.Add(treeItem);

        treeItem = new Class("Class C", greenIconPath);
        treeItem.Students.Add(new Student("Student 1", greenIconPath));
        treeItem.Students.Add(new Student("Student 2", greenIconPath));
        treeItem.Students.Add(new Student("Student 3", greenIconPath));
        classList.Add(treeItem);

        return classList;
    }
}
<TreeView ItemsSource="{Binding ClassList}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}"/>
        </Style>
    </TreeView.ItemContainerStyle>
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type loc:Class}" ItemsSource="{Binding Students}">
            <StackPanel Orientation="Horizontal">
                <Image Margin="2" Source="{Binding ImagePath}">
                </Image>
                <TextBlock Margin="2" Text="{Binding Name}">
                </TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type loc:Student}">
            <StackPanel Orientation="Horizontal">
                <Image Margin="2" Source="{Binding ImagePath}">
                </Image>
                <TextBlock Margin="2" Text="{Binding Name}" ToolTip="{Binding ToolTip}">
                </TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>
public bool IsExpanded
{
    get
    {
        return m_IsExpanded;
    }
    set
    {
        if (Equals(m_IsExpanded, value))
        {
            return;
        }

        m_IsExpanded = value;

        NotifyPropertyChanged("IsExpanded");
    }
}