【问题标题】:Selectable TreeView File Browser MVVM, cant display tree可选择 TreeView 文件浏览器 MVVM,无法显示树
【发布时间】:2014-08-29 22:52:49
【问题描述】:

碰壁,我是 MVVM 结构的新手,需要帮助来显示我的树。我需要显示一个像树一样的文件浏览器。在我搬到 mvvm 后,我的树停止显示。我制作了一个模型,其中包含用于构建文件树的方法。下面是我的模型。

public class UpdateContents 
{
    public string visibleName { get; set; }
    public string fullPath { get; set; }
    public bool isParent { get; private set; }
    public bool? _isSelected { get; set; }
    public UpdateContents _parent;
    public List<UpdateContents> children { get; private set; }
    public bool? isInitiallySelected { get; set; }

    public UpdateContents(string path, bool parent)
    {
        fullPath = path;
        visibleName = path.Substring(path.LastIndexOf('\\') + 1);
        isParent = parent;
        this.children = new List<UpdateContents>();
    }
    public void Initialize()
    {
        foreach (UpdateContents child in this.children)
        {
            child._parent = this;
            child.Initialize();
        }
    }
    public UpdateContents CreateDirectory(string directory)
    {
        UpdateContents groot = new UpdateContents(directory, true);
        groot.isInitiallySelected = true;
        CreateFiles(directory, groot);

        foreach (string dir in Directory.EnumerateDirectories(directory))
        {
            UpdateContents babygroot = new UpdateContents(dir, true);

            CreateFiles(dir, babygroot);
            CreateFolders(dir, babygroot);
            groot.children.Add(babygroot);
        }
        groot.Initialize();
        return groot;
    }

    private void CreateFiles(string path, UpdateContents parent)
    {
        foreach (string file in Directory.EnumerateFiles(path))
        {
            UpdateContents files = new UpdateContents(file, false);

            parent.children.Add(files);
        }
        parent.Initialize();
    }
    private void CreateFolders(string path, UpdateContents Parent)
    {
        foreach (string folder in Directory.EnumerateDirectories(path))
        {
            UpdateContents folders = new UpdateContents(folder, true);
            CreateFiles(folder, folders);
            CreateFolders(folder, folders);
            Parent.children.Add(folders);
        }
        Parent.Initialize();
    }

}

我有一个视图模型,但老实说它没有做任何事情。自从更改为 mvvm 后,我已经对其进行了编辑。下面是视图模型。

    public class MainWindowVM 
{
    public string rootfile { get; set; }
    public string version { get; set; }
    public string date { get; set; }
    public string _name { get; set; }
    public string _path { get; set; }

    public RelayCommand onBrowse { get; set; }

    public MainWindowVM()
    {
        onBrowse = new RelayCommand(onBrowseCommand);
    }

    public void onBrowseCommand ( object commandInvoker )
    {

        Microsoft.Win32.OpenFileDialog win = new Microsoft.Win32.OpenFileDialog();

        Nullable<bool> result = win.ShowDialog();

        if (result == true)
        {
            string filename = win.FileName;
            rootfile= filename;

            rootfile = filename;
            int index = rootfile.Length;

            index = index - 4;
            rootfile = rootfile.Remove(index);

            //Get file version for text box
            var fversion = FileVersionInfo.GetVersionInfo(filename);
            version = fversion.FileVersion;

            //get date created 
            DateTime fdate = File.GetCreationTime(filename);
            date = fdate.ToString();

            //Display Folder Contents
            showzip(filename);

            UpdateContents test = new UpdateContents(rootfile, true);

            UpdateContents directory = test.CreateDirectory(rootfile);          
        }
    }

在我的 XAML 代码中,我只有 TreeViewObject。下面是 xaml 的快照。我知道我需要将 Hierarchialdata 添加到 xaml 但是作为 mvvm 的新手,我不知道如何使用它。

        <DockPanel LastChildFill="True" Grid.Row="4">
        <TreeView DockPanel.Dock="Left" Margin="5,0,5,5" x:Name="foldersItem" ItemsSource="{Binding foldersItem}" >

        </TreeView>
    </DockPanel>

我只需要被发送到正确的方向,看起来我的模型正在构建正确的树结构,但我不知道如何用 mvvm 显示它!该代码现在只会解压缩文件夹。

【问题讨论】:

  • 好文章mvvm。最重要的部分 - INotifyPropertyChanged。另外,您的绑定现在是错误的。

标签: c# wpf xaml mvvm treeview


【解决方案1】:

您需要使用HierarchicalDataTemplate 来获取TreeView 以自动将您的项目一直渲染到树的底部叶节点。

这是来自 MSDN 的一篇文章,解释了所有关于 TreeView 控件和 HierarchicalDataTemplate 对象的内容:How to: Use a TreeView to Display Hierarchical Data

对于 MVVM 和绑定,你需要深入了解 INotifyPropertyChanged 接口:System.ComponentModel.INotifyPropertyChanged

提示: 绑定到 VM 中的任何属性都应在其设置器中引发 PropertyChanged 事件。此外,任何操作都应通过Command 对象(System.Input.ICommand 实现)实现,而不是事件处理程序。

查看 Kent Boogart 从基本原则出发关于 MVVM 的优秀教程:Kent Boogaart's blog - View Models: POCOs versus DependencyObjects

【讨论】:

    猜你喜欢
    • 2014-07-08
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 2022-06-12
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多