using System; using System.Globalization; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; namespace SourceGit.Converters { /// /// Convert depth of a TreeViewItem to Margin property. /// public class TreeViewItemDepthToMargin : IValueConverter { /// /// Indent length /// public double Indent { get; set; } = 19; /// /// Implement IValueConverter.Convert /// /// /// /// /// /// public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { TreeViewItem item = value as TreeViewItem; if (item == null) return new Thickness(0); TreeViewItem iterator = GetParent(item); int depth = 0; while (iterator != null) { depth++; iterator = GetParent(iterator); } return new Thickness(Indent * depth, 0, 0, 0); } /// /// Implement IValueConvert.ConvertBack /// /// /// /// /// /// public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } /// /// Get parent item. /// /// /// private TreeViewItem GetParent(TreeViewItem item) { var parent = VisualTreeHelper.GetParent(item); while (parent != null && !(parent is TreeView) && !(parent is TreeViewItem)) { parent = VisualTreeHelper.GetParent(parent); } return parent as TreeViewItem; } } }