簡體   English   中英

WPF TreeView,IsExpanded 的雙向綁定不會影響 C# 代碼中的 GUI

[英]WPF TreeView, TwoWay binding for IsExpanded is not affecting GUI from C# code

我正在嘗試創建一個可以在樹結構中顯示項目的 TreeView。 我希望能夠使用代碼 (C#) 通過綁定到 ObservableCollection 的屬性來展開和折疊 TreeView 中的 TreeViewItems。

我已經將我的類的一個屬性綁定到 IsExpanded,如果我在設置樹的 ItemSource 之前設置它似乎可以工作 - 新創建的層次結構將預先擴展到。

但是,如果我單擊為集合中的項目設置 IsExpanded 的按鈕,它不會在 GUI 中展開或折疊樹項目。

到目前為止,這是該程序的丑陋屏幕截圖。 這些文件夾是在初始化過程中手動創建的。

非常丑陋的屏幕截圖,所以您可以了解我們正在處理的內容。

這是主窗口中的 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 和天哪,我現在覺得自己是一個非常糟糕的程序員!

為您的MyNode類實現INotifyPropertyChanged接口。 它通知屬性值已更改。

public class MyNode : INotifyPropertyChanged
{
    private bool isExpanded;

    public string Name { get; set; }
    public string Type { get; set; }
    public bool IsExpanded
    {
        get => isExpanded;
        set
        {
            isExpanded = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsExpanded)));
        }
    }
    public ObservableCollection<MyNode> nodes { get; set; }
    public MyNode(string theName, string theType)
    {
        Name = theName;
        Type = theType;
        nodes = new ObservableCollection<MyNode>();
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

您的 MyNode 類需要實現INotifyPropertyChanged以讓 Gui 知道該屬性已更改。 然后在 IsExpanded 屬性的設置器中,您必須調用 NotifyPropertyChanged 已在給定鏈接中進行了解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM