WPF TreeView,IsExpanded的TwoWay绑定不会从C#代码影响GUI

问题描述 投票:0回答:1

我正在尝试创建一个TreeView,该树视图可以显示树结构层次结构中的项目。我希望能够使用代码(C#)通过绑定到ObservableCollection的属性在TreeView中展开和折叠TreeViewItems。

我将类的一个属性绑定到IsExpanded,如果在设置树的ItemSource之前进行设置,它似乎可以工作-新创建的层次结构将进行预先扩展。

但是,如果我单击一个为集合中的项设置IsExpanded的按钮,则它不会在GUI中展开或折叠树项。

这里是该程序的丑陋屏幕截图。这些文件夹是在初始化过程中手动创建的。

Really ugly screenshot, so you get an idea of what we're dealing with.

这是主窗口中的TreeView xaml:

<TreeView x:Name="TheProjectTree" Margin="5" BorderBrush="{x:Null}" ItemsSource="{Binding DataSet}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
            <!--<EventSetter Event="Expanded" Handler="TheProjectTreeItem_Expanded" />-->
        </Style>
    </TreeView.ItemContainerStyle>
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding nodes}">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Path=Icon}" Height="16"/>
                <TextBlock Text=" " />
                <TextBlock Text="{Binding Path=Name}" />
                <TextBlock Text=" (" />
                <TextBlock Text="{Binding Path=Type}" />
                <TextBlock Text=")" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

这里是具有数据结构的MyProject类:

using System.Collections.ObjectModel;

namespace Project_X
{
    public class MyProject
    {
        public ObservableCollection<MyNode> nodes;

        public MyProject()
        {

        }

        public void Initialize()
        {
            nodes = new ObservableCollection<MyNode>();
            nodes.Add(new MyNode("Untitled Project", "Project"));
            AddFolder("0. Initialize");
            AddFolder("1. Reset");
            AddFolder("2. Migrate");
        }

        public void AddFolder(string folderName)
        {
            nodes[0].nodes.Add(new MyProject.MyNode(folderName, "Folder"));
        }

        public class MyNode
        {
            public string Name { get; set; }
            public string Type { get; set; }
            public bool IsExpanded { get; set; }
            public ObservableCollection<MyNode> nodes { get; set; }
            public MyNode(string theName, string theType)
            {
                Name = theName;
                Type = theType;
                nodes = new ObservableCollection<MyNode>();
            }
            public string Icon
            {
                get
                {
                    if (Type == "Project")
                        return "./graphics/icon_projectTree_small.png";
                    else if (Type == "Folder")
                        return "./graphics/icon_projectTree_small.png";
                    else if (Type == "Object")
                        return "./graphics/icon_projectTree_small.png";
                    else if (Type == "SQL")
                        return "./graphics/icon_projectTree_small.png";
                    else if (Type == "Text")
                        return "./graphics/icon_projectTree_small.png";
                    return "./graphics/icon_projectTree_small.png";
                }
            }
        }
    }
}

最后,这是一个可以从测试按钮调用的小测试过程。

private void NewProject()
{
    Project = new MyProject();                      // fire up the main project variable!
    Project.Initialize();                           // give it some dummy data!
    Project.nodes[0].IsExpanded = true;             // pre-expand the top-level project node
    TheProjectTree.ItemsSource = Project.nodes;     // assign the data set to the tree in the main window



    Project.AddFolder("test");                      // this works! adding new folders to the collection will show up in the GUI
    Project.nodes[0].IsExpanded = true;             // this does NOT work! it should collapse the top-levl project node in the tree, but it doesn't
}

如果您能对我的知识有所了解,我将不胜感激。我通常使用SQL,C#和.NET不是我的强项。我整晚都在忙于围绕MVVM和善良工作,我现在感觉自己像是一个简陋的程序员!

c# .net wpf binding treeview
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.