diff --git a/WinUIGallery/Assets/folder.png b/WinUIGallery/Assets/folder.png index dd6d2c51b..f179473a2 100644 Binary files a/WinUIGallery/Assets/folder.png and b/WinUIGallery/Assets/folder.png differ diff --git a/WinUIGallery/ControlPages/TreeViewPage.xaml b/WinUIGallery/ControlPages/TreeViewPage.xaml index 77b2a1915..339e159bf 100644 --- a/WinUIGallery/ControlPages/TreeViewPage.xaml +++ b/WinUIGallery/ControlPages/TreeViewPage.xaml @@ -39,37 +39,41 @@ - + - + + HorizontalAlignment="Center" VerticalAlignment="Top" CanDragItems="True" AllowDrop="True" /> - <TreeView CanDragItems="True" AllowDrop="True"/> + <TreeView x:Name="sampleTreeView" CanDragItems="True" AllowDrop="True"/> - + - + - <TreeView SelectionMode="Multiple" /> + <TreeView x:Name="sampleTreeView" SelectionMode="Multiple" /> - + - + - + - + diff --git a/WinUIGallery/ControlPages/TreeViewPage.xaml.cs b/WinUIGallery/ControlPages/TreeViewPage.xaml.cs index b7270d4cf..71e165201 100644 --- a/WinUIGallery/ControlPages/TreeViewPage.xaml.cs +++ b/WinUIGallery/ControlPages/TreeViewPage.xaml.cs @@ -1,15 +1,11 @@ -using System; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using System.Collections.ObjectModel; -using System.ComponentModel; namespace WinUIGallery.ControlPages { public sealed partial class TreeViewPage : Page { - TreeViewNode personalFolder; - TreeViewNode personalFolder2; private ObservableCollection DataSource; public TreeViewPage() @@ -18,190 +14,70 @@ public TreeViewPage() this.DataContext = this; DataSource = GetData(); - InitializeSampleTreeView(); - InitializeSampleTreeView2(); + InitializeSampleTreeView(sampleTreeView); + InitializeSampleTreeView(sampleTreeView2); } - private void InitializeSampleTreeView() + private void InitializeSampleTreeView(TreeView sampleTreeView) { TreeViewNode workFolder = new TreeViewNode() { Content = "Work Documents" }; workFolder.IsExpanded = true; workFolder.Children.Add(new TreeViewNode() { Content = "XYZ Functional Spec" }); workFolder.Children.Add(new TreeViewNode() { Content = "Feature Schedule" }); - workFolder.Children.Add(new TreeViewNode() { Content = "Overall Project Plan" }); - workFolder.Children.Add(new TreeViewNode() { Content = "Feature Resources Allocation" }); TreeViewNode remodelFolder = new TreeViewNode() { Content = "Home Remodel" }; remodelFolder.IsExpanded = true; remodelFolder.Children.Add(new TreeViewNode() { Content = "Contractor Contact Info" }); remodelFolder.Children.Add(new TreeViewNode() { Content = "Paint Color Scheme" }); - remodelFolder.Children.Add(new TreeViewNode() { Content = "Flooring woodgrain type" }); - remodelFolder.Children.Add(new TreeViewNode() { Content = "Kitchen cabinet style" }); - personalFolder = new TreeViewNode() { Content = "Personal Documents" }; + TreeViewNode personalFolder = new TreeViewNode() { Content = "Personal Documents" }; personalFolder.IsExpanded = true; personalFolder.Children.Add(remodelFolder); sampleTreeView.RootNodes.Add(workFolder); sampleTreeView.RootNodes.Add(personalFolder); } - private void InitializeSampleTreeView2() - { - TreeViewNode workFolder = new TreeViewNode() { Content = "Work Documents" }; - workFolder.IsExpanded = true; - - workFolder.Children.Add(new TreeViewNode() { Content = "XYZ Functional Spec" }); - workFolder.Children.Add(new TreeViewNode() { Content = "Feature Schedule" }); - workFolder.Children.Add(new TreeViewNode() { Content = "Overall Project Plan" }); - workFolder.Children.Add(new TreeViewNode() { Content = "Feature Resources Allocation" }); - TreeViewNode remodelFolder = new TreeViewNode() { Content = "Home Remodel" }; - remodelFolder.IsExpanded = true; - - remodelFolder.Children.Add(new TreeViewNode() { Content = "Contractor Contact Info" }); - remodelFolder.Children.Add(new TreeViewNode() { Content = "Paint Color Scheme" }); - remodelFolder.Children.Add(new TreeViewNode() { Content = "Flooring woodgrain type" }); - remodelFolder.Children.Add(new TreeViewNode() { Content = "Kitchen cabinet style" }); - - personalFolder2 = new TreeViewNode() { Content = "Personal Documents" }; - personalFolder2.IsExpanded = true; - personalFolder2.Children.Add(remodelFolder); - - sampleTreeView2.RootNodes.Add(workFolder); - sampleTreeView2.RootNodes.Add(personalFolder2); - } - - private void sampleTreeView_ItemInvoked(TreeView sender, TreeViewItemInvokedEventArgs args) - { - return; - } - private ObservableCollection GetData() { - var list = new ObservableCollection(); - ExplorerItem folder1 = new ExplorerItem() + return new ObservableCollection { - Name = "Work Documents", - Type = ExplorerItem.ExplorerItemType.Folder, - Children = + new ExplorerItem { - new ExplorerItem() - { - Name = "Functional Specifications", - Type = ExplorerItem.ExplorerItemType.Folder, - Children = - { - new ExplorerItem() - { - Name = "TreeView spec", - Type = ExplorerItem.ExplorerItemType.File, - } - } - }, - new ExplorerItem() - { - Name = "Feature Schedule", - Type = ExplorerItem.ExplorerItemType.File, - }, - new ExplorerItem() + Name = "Documents", + Type = ExplorerItem.ExplorerItemType.Folder, + Children = { - Name = "Overall Project Plan", - Type = ExplorerItem.ExplorerItemType.File, - }, - new ExplorerItem() + new ExplorerItem { Name = "ProjectProposal", Type = ExplorerItem.ExplorerItemType.File }, + new ExplorerItem { Name = "BudgetReport", Type = ExplorerItem.ExplorerItemType.File } + } + }, + new ExplorerItem + { + Name = "Projects", + Type = ExplorerItem.ExplorerItemType.Folder, + Children = { - Name = "Feature Resources Allocation", - Type = ExplorerItem.ExplorerItemType.File, + new ExplorerItem { Name = "Project Plan", Type = ExplorerItem.ExplorerItemType.File } } } }; - ExplorerItem folder2 = new ExplorerItem() - { - Name = "Personal Folder", - Type = ExplorerItem.ExplorerItemType.Folder, - Children = - { - new ExplorerItem() - { - Name = "Home Remodel Folder", - Type = ExplorerItem.ExplorerItemType.Folder, - Children = - { - new ExplorerItem() - { - Name = "Contractor Contact Info", - Type = ExplorerItem.ExplorerItemType.File, - }, - new ExplorerItem() - { - Name = "Paint Color Scheme", - Type = ExplorerItem.ExplorerItemType.File, - }, - new ExplorerItem() - { - Name = "Flooring Woodgrain type", - Type = ExplorerItem.ExplorerItemType.File, - }, - new ExplorerItem() - { - Name = "Kitchen Cabinet Style", - Type = ExplorerItem.ExplorerItemType.File, - } - } - } - } - }; - - list.Add(folder1); - list.Add(folder2); - return list; } - } - public class ExplorerItem : INotifyPropertyChanged + public class ExplorerItem { - public event PropertyChangedEventHandler PropertyChanged; - public enum ExplorerItemType { Folder, File }; - public string Name { get; set; } - public ExplorerItemType Type { get; set; } - private ObservableCollection m_children; - public ObservableCollection Children - { - get - { - if (m_children == null) - { - m_children = new ObservableCollection(); - } - return m_children; - } - set - { - m_children = value; - } - } - - private bool m_isExpanded; - public bool IsExpanded + public enum ExplorerItemType { - get { return m_isExpanded; } - set - { - if (m_isExpanded != value) - { - m_isExpanded = value; - NotifyPropertyChanged("IsExpanded"); - } - } + Folder, + File, } - private void NotifyPropertyChanged(string propertyName) - { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } + public string Name { get; set; } + public ExplorerItemType Type { get; set; } + public ObservableCollection Children { get; set; } = new ObservableCollection(); } class ExplorerItemTemplateSelector : DataTemplateSelector diff --git a/WinUIGallery/ControlPagesSampleCode/TreeView/TreeViewDataBindingSample_cs.txt b/WinUIGallery/ControlPagesSampleCode/TreeView/TreeViewDataBindingSample_cs.txt new file mode 100644 index 000000000..9234dc854 --- /dev/null +++ b/WinUIGallery/ControlPagesSampleCode/TreeView/TreeViewDataBindingSample_cs.txt @@ -0,0 +1,82 @@ +using System.Collections.ObjectModel; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; + +namespace YourNamespace +{ + public sealed partial class YourPage : Page + { + // DataSource is the data collection that will be bound to the TreeView's ItemsSource. + public ObservableCollection DataSource { get; set; } + + public YourPage() + { + this.InitializeComponent(); + + // Initialize the data source with sample data and set it as the context for data binding. + DataSource = GetData(); + this.DataContext = this; // Bind the DataContext of the page to itself for XAML bindings. + } + + // Method to provide sample data for the TreeView. + private ObservableCollection GetData() + { + return new ObservableCollection + { + // Root folder with child files. + new ExplorerItem + { + Name = "Documents", + Type = ExplorerItem.ExplorerItemType.Folder, + Children = + { + new ExplorerItem + { + Name = "ProjectProposal", + Type = ExplorerItem.ExplorerItemType.File, + }, + new ExplorerItem + { + Name = "BudgetReport", + Type = ExplorerItem.ExplorerItemType.File, + }, + }, + }, + // Another root folder with one child file. + new ExplorerItem + { + Name = "Projects", + Type = ExplorerItem.ExplorerItemType.Folder, + Children = + { + new ExplorerItem + { + Name = "Project Plan", + Type = ExplorerItem.ExplorerItemType.File, + }, + }, + }, + }; + } + } + + // Class to represent items in the TreeView. + public class ExplorerItem + { + // Enum to define the type of the item: Folder or File. + public enum ExplorerItemType + { + Folder, + File, + } + + // Name of the item (displayed in the TreeView). + public string Name { get; set; } + + // Type of the item (Folder or File). + public ExplorerItemType Type { get; set; } + + // Collection of child items. Used for nested nodes in the TreeView. + public ObservableCollection Children { get; set; } = new ObservableCollection(); + } +} \ No newline at end of file diff --git a/WinUIGallery/ControlPagesSampleCode/TreeView/TreeViewSample1_cs.txt b/WinUIGallery/ControlPagesSampleCode/TreeView/TreeViewSample1_cs.txt new file mode 100644 index 000000000..a890f21c9 --- /dev/null +++ b/WinUIGallery/ControlPagesSampleCode/TreeView/TreeViewSample1_cs.txt @@ -0,0 +1,27 @@ +private void InitializeSampleTreeView(TreeView sampleTreeView) +{ + // Create a root node with initial content and set it to be expanded. + TreeViewNode workNode = new TreeViewNode() { Content = "Work Documents" }; + workNode.IsExpanded = true; + + // Add child nodes with content related to the root node. + workNode.Children.Add(new TreeViewNode() { Content = "XYZ Functional Spec" }); + workNode.Children.Add(new TreeViewNode() { Content = "Feature Schedule" }); + + // Create another node with initial content and set it to be expanded. + TreeViewNode remodelNode = new TreeViewNode() { Content = "Home Remodel" }; + remodelNode.IsExpanded = true; + + // Add child nodes with specific content under this node. + remodelNode.Children.Add(new TreeViewNode() { Content = "Contractor Contact Info" }); + remodelNode.Children.Add(new TreeViewNode() { Content = "Paint Color Scheme" }); + + // Create a node with broader content that includes the previous node as a child. + TreeViewNode personalNode = new TreeViewNode() { Content = "Personal Documents" }; + personalNode.IsExpanded = true; + personalNode.Children.Add(remodelNode); + + // Add the main nodes to the TreeView's root. + sampleTreeView.RootNodes.Add(workNode); + sampleTreeView.RootNodes.Add(personalNode); +} \ No newline at end of file diff --git a/WinUIGallery/ControlPagesSampleCode/TreeView/TreeViewTemplateSelectorSample_cs.txt b/WinUIGallery/ControlPagesSampleCode/TreeView/TreeViewTemplateSelectorSample_cs.txt new file mode 100644 index 000000000..047c92389 --- /dev/null +++ b/WinUIGallery/ControlPagesSampleCode/TreeView/TreeViewTemplateSelectorSample_cs.txt @@ -0,0 +1,90 @@ +using System.Collections.ObjectModel; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; + +namespace YourNamespace +{ + public sealed partial class YourPage : Page + { + public ObservableCollection DataSource { get; set; } + + public YourPage() + { + this.InitializeComponent(); + DataSource = GetData(); + this.DataContext = this; + } + + private ObservableCollection GetData() + { + return new ObservableCollection + { + new ExplorerItem + { + Name = "Documents", + Type = ExplorerItem.ExplorerItemType.Folder, + Children = + { + new ExplorerItem + { + Name = "ProjectProposal", + Type = ExplorerItem.ExplorerItemType.File, + }, + new ExplorerItem + { + Name = "BudgetReport", + Type = ExplorerItem.ExplorerItemType.File, + }, + }, + }, + new ExplorerItem + { + Name = "Projects", + Type = ExplorerItem.ExplorerItemType.Folder, + Children = + { + new ExplorerItem + { + Name = "Project Plan", + Type = ExplorerItem.ExplorerItemType.File, + }, + }, + }, + }; + } + } + + public class ExplorerItem + { + public enum ExplorerItemType + { + Folder, + File, + } + + public string Name { get; set; } + public ExplorerItemType Type { get; set; } + public ObservableCollection Children { get; set; } = new ObservableCollection(); + } + + class ExplorerItemTemplateSelector : DataTemplateSelector + { + // Template to use for folder items in the TreeView. + public DataTemplate FolderTemplate { get; set; } + + // Template to use for file items in the TreeView. + public DataTemplate FileTemplate { get; set; } + + // Determines which template to use for each item in the TreeView based on its type. + protected override DataTemplate SelectTemplateCore(object item) + { + // Cast the item to the ExplorerItem type. + var explorerItem = (ExplorerItem)item; + + // Return the appropriate template: FolderTemplate for folders, FileTemplate for files. + return explorerItem.Type == ExplorerItem.ExplorerItemType.Folder + ? FolderTemplate + : FileTemplate; + } + } +} \ No newline at end of file diff --git a/WinUIGallery/ControlPagesSampleCode/TreeView/TreeViewTemplateSelectorSample_xaml.txt b/WinUIGallery/ControlPagesSampleCode/TreeView/TreeViewTemplateSelectorSample_xaml.txt index 6e73966ae..6327d9035 100644 --- a/WinUIGallery/ControlPagesSampleCode/TreeView/TreeViewTemplateSelectorSample_xaml.txt +++ b/WinUIGallery/ControlPagesSampleCode/TreeView/TreeViewTemplateSelectorSample_xaml.txt @@ -1,30 +1,37 @@ - - - - - - - - - - + - - - - - - - - - + + + + + + + + + + + - - + + + + + + + + + + - + + + + + + \ No newline at end of file diff --git a/WinUIGallery/WinUIGallery.csproj b/WinUIGallery/WinUIGallery.csproj index 80e793fd4..c100b5942 100644 --- a/WinUIGallery/WinUIGallery.csproj +++ b/WinUIGallery/WinUIGallery.csproj @@ -150,6 +150,7 @@ + @@ -287,6 +288,9 @@ + + + @@ -315,6 +319,7 @@ + @@ -459,6 +464,9 @@ + + +