可选的TreeView文件浏览器MVVM,无法显示树

加勒特·明基

碰壁,我是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显示它!该代码目前仅将文件夹解压缩。

Toadflakz

你需要使用一个HierarchicalDataTemplate获得TreeView自动渲染你的项目一路下跌到你的树底部的叶节点。

这是MSDN上的一篇文章,全面解释了TreeView控件和HierarchicalDataTemplate对象:如何:使用TreeView显示层次结构数据

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

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

从基本原理中查看Kent Boogart关于MVVM的出色教程:Kent Boogaart的博客-视图模型:POCO与DependencyObjects

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PDF文件无法在浏览器中正确显示

来自分类Dev

浏览器无法显示文字

来自分类Dev

浏览器无法正确显示字体

来自分类Dev

无法从Android浏览器上传文件

来自分类Dev

文件浏览器无法启动

来自分类Dev

无法通过 webapi 在浏览器 angularjs 中显示文件

来自分类Dev

无法使用响应对象在浏览器中显示 pdf 文件

来自分类Dev

ERb文件在浏览器中显示为HTML文件

来自分类Dev

HTML文档无法在OSX的浏览器上正确显示

来自分类Dev

非Webkit浏览器无法正确显示特殊字符

来自分类Dev

HTML:无法在浏览器上显示吗?

来自分类Dev

PDFObject在浏览器中无法正确显示

来自分类Dev

RedisInsight Docker显示:无法在浏览器中访问站点

来自分类Dev

日期时间模型无法显示在谷歌浏览器上

来自分类Dev

htmlspecialchars无法在浏览器中正确显示

来自分类Dev

HTML:无法在浏览器上显示吗?

来自分类Dev

IIS中的安装站点无法在浏览器中显示

来自分类Dev

Java:无法在浏览器上显示 ADF 控件值

来自分类Dev

文件被下载而不是显示在浏览器中

来自分类Dev

浏览器中的Azure文件存储URL显示InvalidHeaderValue

来自分类Dev

Web浏览器未显示svg文件

来自分类Dev

如何使用Web浏览器对象显示pdf文件

来自分类Dev

在Eclipse包浏览器中显示打开的文件

来自分类Dev

Node.js在浏览器中显示JSON文件

来自分类Dev

如何在浏览器上显示任何文件

来自分类Dev

Web浏览器显示RAR文件而不是下载它们

来自分类Dev

文件被下载而不是显示在浏览器中

来自分类Dev

在浏览器C#中显示文件

来自分类Dev

Webpack:浏览器DevTool中未显示文件