Nie rozumiem takiego kodu źródłowego z <Window.DataContext>:
<Window x:Class="Hierarchy.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Hierarchy"
mc:Ignorable="d"
Title="MainWindow" Height="453.583" Width="525">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
...
</Window>
Chodzi o to, że główny form ma zapięty ViewModel i ja się pytam po co? Teraz nie mogę stworzyć przycisku, który np dodaje coś do m_folders.
Kod tego ViewModelu poniżej:
namespace Hierarchy
{
class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
TEST = "jubba";
m_folders = new List<IFolder>();
//add Root items
Folders.Add(new Folder { FolderLabel = "Dummy1", FullPath = @"C:\dummy1" });
Folders.Add(new Folder { FolderLabel = "Dummy2", FullPath = @"C:\dummy2" });
Folders.Add(new Folder { FolderLabel = "Dummy3", FullPath = @"C:\dummy3" });
Folders.Add(new Folder { FolderLabel = "Dummy4", FullPath = @"C:\dummy4" });
//add sub items
Folders[0].Folders.Add(new Folder { FolderLabel = "Dummy11", FullPath = @"C:\dummy11" });
Folders[0].Folders.Add(new Folder { FolderLabel = "Dummy12", FullPath = @"C:\dummy12" });
Folders[0].Folders.Add(new Folder { FolderLabel = "Dummy13", FullPath = @"C:\dummy13" });
Folders[0].Folders.Add(new Folder { FolderLabel = "Dummy14", FullPath = @"C:\dummy14" });
Folders[0].Folders.Add(new Folder { FolderLabel = "Dummy24", FullPath = @"C:\dummy14" });
Folders[0].Folders[0].Folders.Add(new Folder { FolderLabel = "xxxxx", FullPath = @"C:\dummy14" });
Folders[0].Folders[0].Folders.Add(new Folder { FolderLabel = "vcxvx", FullPath = @"C:\dummy14" });
Folders[0].Folders[0].Folders.Add(new Folder { FolderLabel = "vvxcvxcvcx", FullPath = @"C:\dummy14" });
}
public string TEST { get; set; }
private List<IFolder> m_folders;
public List<IFolder> 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;
}
}
Kod pochodzi z tego przykładu:
http://blog.clauskonrad.net/2011/04/how-to-make-hierarchical-treeview.html