If the 'Children' property on any of those contained objects of type ChildLevel1ViewModel, those will also automatically generate - but this time, the TextBlock on my TreeNode will bind to a property on those ChildLevel1ViewModel. For example, I could have an ObservableCollection that contained objects of type ParentTreeViewModel1, ParentTreeViewModel2, and ParentTreeViewModel3. In this scenario, if I build up ViewModel classes, I can auto-generate as many items on the tree that I want. Take for example a TreeView with XAML along the lines of this. The big part of that is the automatic generation of new items. That said, at a glance I'm not really sure if your code would or wouldn't work, but I think you're missing what's possible with those HierarchicalDataTemplates (or perhaps you aren't missing it, and simply don't care) and the magic of DataBinding. I think that 'breaking MVVM' is really subjective and people can be pretty opinionated about that. Simplifying the WPF TreeView by Using the ViewModel Pattern I don't have any formal training with programming so if this is a stupid question I apologize.įirst off, I found this article very helpful in understanding how to use the WPF TreeView and how to make use of it: Would this work? Does this break MVVM? Is it a generally stupid idea? Then I would bind the tree view in the View to this tree view. TVI.Header = Path.GetFileNameWithoutExtension(dir) Add the top level directory as a tree view item under the tree view. Start moving through each top level directory within the directory path.įoreach (var dir in Directory.GetDirectories(DirectoryPath)) TreeView SchemaContainer = new TreeView() NotifyOfPropert圜hange(() => DirectorySchema) I was hoping to dynamically create a tree view and populate it in the ViewModel and "bind" a holder tree view in the View to this dynamically created tree view.Įssentially I'm looking to have a FOREACH loop similar to: private TreeView _directorySchema I had an idea of how to handle populating the tree view in a way that would be familiar to me and my own WinForm days. It seems like a lot of work for such a simple task. I've done some reading about implementing the 'HierarchicalDataTemplate' via XAML for creating a tree view of a directory (showing sub-directories and files). So, awhile back I loved using tree views in WinForms and had an idea for a program that would involve a tree view in the WPF/MVVM format.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |