alielie
2026-06-04 771a0e7b6b1276087515bbe94de28358cb92963b
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
using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
 
namespace HMI.UI
{
    public class TreeDataGrid : DataGrid
    {
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<bool> ExpandedChange;
 
        public virtual void OnExpandedChange(bool expand)
        {
            ExpandedChange?.Invoke(this, expand);
        }
 
        static TreeDataGrid()
        {
            ScrollViewer.HorizontalScrollBarVisibilityProperty
                .OverrideMetadata(typeof(TreeDataGrid), new FrameworkPropertyMetadata(ScrollBarVisibility.Disabled));
            ScrollViewer.VerticalScrollBarVisibilityProperty
                .OverrideMetadata(typeof(TreeDataGrid), new FrameworkPropertyMetadata(ScrollBarVisibility.Disabled));
        }
 
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new TreeDataGridRow();
        }
 
        protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
        {
            base.OnItemsSourceChanged(oldValue, newValue);
        }
    }
 
    public class TreeDataGridRow : DataGridRow
    {
 
        public TreeDataGridRow()
        {
            this.DataContextChanged += TreeGridItem_DataContextChanged;
        }
 
        private void TreeGridItem_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue != null && e.OldValue is TreeItemData oldTreeData)
            {
                oldTreeData.IsExpandedChange -= DataIsExpandedChange;
                oldTreeData.VisibleChange -= DataVisiableChange;
            }
            if (DataContext != null && DataContext is TreeItemData treeData)
            {
                Visibility = treeData.IsVisible ? Visibility.Visible : Visibility.Collapsed;
                treeData.IsExpandedChange += DataIsExpandedChange;
                treeData.VisibleChange += DataVisiableChange;
            }
        }
 
        private void DataIsExpandedChange(object sender, bool IsExpanded)
        {
            object parent = ItemsControl.ItemsControlFromItemContainer(this);
            if (sender is TreeItemData tid)
            {
                foreach (var data in tid.Items)
                {
                    data.IsVisible = IsExpanded;
                }
            }
        }
 
        private void DataVisiableChange(object sender, bool visiable)
        {
            this.Visibility = visiable ? Visibility.Visible : Visibility.Collapsed;
            //if (parent != null && parent is TreeDataGrid treeDataGrid)
            //{
            //    treeDataGrid.OnExpandedChange(visiable);
            //}
        }
    }
}