aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/MaterialDesignInXamlToolkit-master/MaterialDesignThemes.Wpf/TreeViewAssist.cs
blob: 04d470345c463ff25c8604dc0527309de363052a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace MaterialDesignThemes.Wpf
{
    public static class TreeViewAssist
    {
        /// <summary>
        /// Allows additional rendering for each tree node, outside of the rippled part of the node which responsds to user selection.                        
        /// </summary>
        /// <remarks>
        /// The content to be rendered is the same of the <see cref="TreeViewItem"/>; i.e the Header property, or
        /// some other content such as a view model, typically when using a <see cref="HierarchicalDataTemplate"/>.
        /// </remarks>
        public static readonly DependencyProperty AdditionalTemplateProperty = DependencyProperty.RegisterAttached(
            "AdditionalTemplate",
            typeof(DataTemplate),
            typeof(TreeViewAssist),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));

        /// <summary>
        /// Sets the additional template.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">The value.</param>
        public static void SetAdditionalTemplate(DependencyObject element, DataTemplate value)
        {
            element.SetValue(AdditionalTemplateProperty, value);
        }

        /// <summary>
        /// Gets the additional template.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>
        /// The <see cref="DataTemplate" />.
        /// </returns>
        public static DataTemplate GetAdditionalTemplate(DependencyObject element)
        {
            return (DataTemplate)element.GetValue(AdditionalTemplateProperty);
        }

        /// <summary>
        /// Allows additional rendering for each tree node, outside of the rippled part of the node which responsds to user selection.                        
        /// </summary>
        /// <remarks>
        /// The content to be rendered is the same of the <see cref="TreeViewItem"/>; i.e the Header property, or
        /// some other content such as a view model, typically when using a <see cref="HierarchicalDataTemplate"/>.
        /// </remarks>
        public static readonly DependencyProperty AdditionalTemplateSelectorProperty = DependencyProperty.RegisterAttached(
            "AdditionalTemplateSelector",
            typeof(DataTemplateSelector),
            typeof(TreeViewAssist),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));

        /// <summary>
        /// Sets the additional template selector.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">The value.</param>
        public static void SetAdditionalTemplateSelector(DependencyObject element, DataTemplateSelector value)
        {
            element.SetValue(AdditionalTemplateSelectorProperty, value);
        }

        /// <summary>
        /// Gets the additional template selector.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>
        /// The <see cref="DataTemplateSelector" />.
        /// </returns>
        public static DataTemplateSelector GetAdditionalTemplateSelector(DependencyObject element)
        {
            return (DataTemplateSelector)element.GetValue(AdditionalTemplateSelectorProperty);
        }

        private static readonly Lazy<DataTemplate> NoAdditionalTemplateProvider = new Lazy<DataTemplate>(CreateEmptyGridDataTemplate);

        /// <summary>
        /// To be used at <see cref="TreeViewItem"/> level, or to be returned by <see cref="AdditionalTemplateSelector"/>
        /// implementors when the additional template associated with a tree should not be used.
        /// </summary>
        public static readonly DataTemplate SuppressAdditionalTemplate = NoAdditionalTemplateProvider.Value;

        public static DataTemplate CreateEmptyGridDataTemplate()
        {            
            var xaml = "<DataTemplate><Grid /></DataTemplate>";
            var parserContext = new ParserContext();
            parserContext.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            parserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");

            using (var memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(xaml)))
            {
                return (DataTemplate)XamlReader.Load(memoryStream, parserContext);
            }            
        }
    }
}