【问题标题】:Hierarchical bind in WPF TreeView not working on button clickWPF TreeView中的分层绑定在按钮单击时不起作用
【发布时间】:2014-10-22 19:16:44
【问题描述】:

我有以下 ViewModel 类:

public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        m_folders = new List<Folder>();
    }

    private List<Folder> m_folders;
    public List<Folder> Folders
    {
        get { return m_folders; }
        set
        {
            m_folders = value;
            NotifiyPropertyChanged("Folders");
        }
    }

    void NotifiyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

还有文件夹类:

public class Folder
{
    public Folder()
    {
        Folders = new List<Folder>();
    }

    public string FullPath
    {
        get;
        set;
    }

    public string FolderLabel
    {
        get;
        set;
    }

    public List<Folder> Folders
    {
        get;
        set;
    }
}

ViewModel 类绑定到 TreeView,如下所示:

    <TreeView ItemsSource="{Binding Folders}" Margin="10" Height="200" Name="treeView">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Folders}" DataType="{x:Type local:Folder}" >
                <TextBlock Text="{Binding FolderLabel}"/>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

绑定本身在 Window Load 事件中完成,然后我填充对象:

    public ViewModel model = new ViewModel();

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        treeView.DataContext = model;

        //add Root items
        model.Folders.Add(new Folder { FolderLabel = "Dummy1", FullPath = @"C:\dummy1" });
        model.Folders.Add(new Folder { FolderLabel = "Dummy2", FullPath = @"C:\dummy2" });
        model.Folders.Add(new Folder { FolderLabel = "Dummy3", FullPath = @"C:\dummy3" });
        model.Folders.Add(new Folder { FolderLabel = "Dummy4", FullPath = @"C:\dummy4" });

        //add sub items
        model.Folders[0].Folders.Add(new Folder { FolderLabel = "Dummy11", FullPath = @"C:\dummy11" });
        model.Folders[0].Folders.Add(new Folder { FolderLabel = "Dummy12", FullPath = @"C:\dummy12" });
        model.Folders[0].Folders.Add(new Folder { FolderLabel = "Dummy13", FullPath = @"C:\dummy13" });
        model.Folders[0].Folders.Add(new Folder { FolderLabel = "Dummy14", FullPath = @"C:\dummy14" });

        model.Folders[0].Folders[0].Folders.Add(new Folder { FolderLabel = "MyDummy1", FullPath = @"xxxxxx" });
    }

到目前为止,一切都很好。一切都很好。然后我向表单添加一个按钮并从表单加载中删除填充代码并将其放在按钮单击上。它不起作用了。 TreeView 绑定不再起作用。我做错了吗?

【问题讨论】:

    标签: c# .net wpf treeview


    【解决方案1】:

    因为您将 TreeView 的项目源绑定到 List&lt;Folder&gt;。初始绑定将起作用,但进一步的更新不会反映在 UI 中,因为 List&lt;T&gt; 内部不实现 INotifyPropertyChangedINotifyCollectionChanged 接口。您需要将List&lt;Folder&gt; 替换为ObservableCollection&lt;Folder&gt;

    【讨论】:

    • 现在就像一个魅力。也谢谢你的解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 2015-12-09
    相关资源
    最近更新 更多