命中一堵牆,我是新的MVVM結構,需要幫助顯示我的樹。我需要顯示一個像樹一樣的文件瀏覽器。在我轉移到mvvm後,我的樹停止顯示。我用它裏面的方法建立了一個模型來構建一個文件樹。下面是我的模型。可選的TreeView文件瀏覽器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(http://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern-in- WPF /)。最重要的部分 - 「INotifyPropertyChanged」。另外,你的綁定現在是錯誤的。 – cdmnk 2014-08-29 23:01:27