|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Windows; |
| 8 | +using System.Windows.Controls; |
| 9 | +using System.Windows.Markup; |
| 10 | + |
| 11 | +namespace MaterialDesignThemes.Wpf |
| 12 | +{ |
| 13 | + public static class TreeViewAssist |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Allows additional rendering for each tree node, outside of the rippled part of the node which responsds to user selection. |
| 17 | + /// </summary> |
| 18 | + /// <remarks> |
| 19 | + /// The content to be rendered is the same of the <see cref="TreeViewItem"/>; i.e the Header property, or |
| 20 | + /// some other content such as a view model, typically when using a <see cref="HierarchicalDataTemplate"/>. |
| 21 | + /// </remarks> |
| 22 | + public static readonly DependencyProperty AdditionalTemplateProperty = DependencyProperty.RegisterAttached( |
| 23 | + "AdditionalTemplate", |
| 24 | + typeof(DataTemplate), |
| 25 | + typeof(TreeViewAssist), |
| 26 | + new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits)); |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Sets the additional template. |
| 30 | + /// </summary> |
| 31 | + /// <param name="element">The element.</param> |
| 32 | + /// <param name="value">The value.</param> |
| 33 | + public static void SetAdditionalTemplate(DependencyObject element, DataTemplate value) |
| 34 | + { |
| 35 | + element.SetValue(AdditionalTemplateProperty, value); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Gets the additional template. |
| 40 | + /// </summary> |
| 41 | + /// <param name="element">The element.</param> |
| 42 | + /// <returns> |
| 43 | + /// The <see cref="DataTemplate" />. |
| 44 | + /// </returns> |
| 45 | + public static DataTemplate GetAdditionalTemplate(DependencyObject element) |
| 46 | + { |
| 47 | + return (DataTemplate)element.GetValue(AdditionalTemplateProperty); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Allows additional rendering for each tree node, outside of the rippled part of the node which responsds to user selection. |
| 52 | + /// </summary> |
| 53 | + /// <remarks> |
| 54 | + /// The content to be rendered is the same of the <see cref="TreeViewItem"/>; i.e the Header property, or |
| 55 | + /// some other content such as a view model, typically when using a <see cref="HierarchicalDataTemplate"/>. |
| 56 | + /// </remarks> |
| 57 | + public static readonly DependencyProperty AdditionalTemplateSelectorProperty = DependencyProperty.RegisterAttached( |
| 58 | + "AdditionalTemplateSelector", |
| 59 | + typeof(DataTemplateSelector), |
| 60 | + typeof(TreeViewAssist), |
| 61 | + new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits)); |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Sets the additional template selector. |
| 65 | + /// </summary> |
| 66 | + /// <param name="element">The element.</param> |
| 67 | + /// <param name="value">The value.</param> |
| 68 | + public static void SetAdditionalTemplateSelector(DependencyObject element, DataTemplateSelector value) |
| 69 | + { |
| 70 | + element.SetValue(AdditionalTemplateSelectorProperty, value); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Gets the additional template selector. |
| 75 | + /// </summary> |
| 76 | + /// <param name="element">The element.</param> |
| 77 | + /// <returns> |
| 78 | + /// The <see cref="DataTemplateSelector" />. |
| 79 | + /// </returns> |
| 80 | + public static DataTemplateSelector GetAdditionalTemplateSelector(DependencyObject element) |
| 81 | + { |
| 82 | + return (DataTemplateSelector)element.GetValue(AdditionalTemplateSelectorProperty); |
| 83 | + } |
| 84 | + |
| 85 | + private static readonly Lazy<DataTemplate> NoAdditionalTemplateProvider = new Lazy<DataTemplate>(CreateEmptyGridDataTemplate); |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// To be used at <see cref="TreeViewItem"/> level, or to be returned by <see cref="AdditionalTemplateSelector"/> |
| 89 | + /// implementors when the additional template associated with a tree should not be used. |
| 90 | + /// </summary> |
| 91 | + public static readonly DataTemplate SuppressAdditionalTemplate = NoAdditionalTemplateProvider.Value; |
| 92 | + |
| 93 | + public static DataTemplate CreateEmptyGridDataTemplate() |
| 94 | + { |
| 95 | + var xaml = "<DataTemplate><Grid /></DataTemplate>"; |
| 96 | + var parserContext = new ParserContext(); |
| 97 | + parserContext.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"); |
| 98 | + parserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml"); |
| 99 | + |
| 100 | + using (var memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(xaml))) |
| 101 | + { |
| 102 | + return (DataTemplate)XamlReader.Load(memoryStream, parserContext); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments