alielie
2026-09-10 9ec80cafc23f5ee3278cacce3c9f86f4087b435a
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
 
namespace UILib
{
    public class TreeViewColumnWidthHelper
    {
        public static GridLength? GetAutoWidth(GridViewColumn column)
        {
            return (GridLength?)column.GetValue(AutoWidthProperty);
        }
 
        public static void SetAutoWidth(GridViewColumn column, GridLength? length)
        {
            column.SetValue(AutoWidthProperty, length);
        }
 
        public static readonly DependencyProperty AutoWidthProperty =
            DependencyProperty.RegisterAttached("AutoWidth", typeof(GridLength?), typeof(GridViewColumn),
            new PropertyMetadata(null));
    }
 
    public class AlternatingRowConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is TreeViewItem item)
            {
                int globalIndex = GetGlobalIndex(item);
                return globalIndex % 2 == 0 ? Brushes.White : new SolidColorBrush(Color.FromRgb(245, 245, 245));
            }
            return Brushes.White;
        }
 
        private int GetGlobalIndex(TreeViewItem item)
        {
            ItemsControl parent = ItemsControl.ItemsControlFromItemContainer(item);
            if (parent == null) return 0;
 
            // 获取当前item在父容器中的索引
            int indexInParent;
            if (parent is TreeViewItem treeViewItem)
            {
                indexInParent = GetGlobalIndex(treeViewItem);
            }
            else
            {
                indexInParent = 0;
            }
 
            // 父级内索引 * 2 + 当前索引
            var parentItems = parent.Items;
            int currentIndex = parentItems.IndexOf(item.DataContext);
 
            return indexInParent * 2 + currentIndex;
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            => throw new NotImplementedException();
    }
 
    /// <summary>
    /// 按钮样式转换
    /// </summary>
    public class SimpleTreeListViewButtonConverter : IMultiValueConverter
    {
        public static SimpleTreeListViewButtonConverter Instance = new SimpleTreeListViewButtonConverter();
        public static readonly double sqrt2 = Math.Sqrt(2);
 
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values[0] is TreeListViewItem tlvi)
            {
                if (values[1] is int level && parameter.ToString() == "Size")
                {
                    double BaseSize = (double)values[2];
                    return (level + 1) * BaseSize;
                }
 
 
                if (values[1] is int level1 && values[2] is int index
                    && values[3] is bool IsExpanded
                    && values[4] is bool IsLastNode
                     && values[5] is bool HasItems && parameter.ToString() == "Content")
                {
                    double BaseSize = (double)values[6];
                    double BaseHalfSize = BaseSize / 2;
                    double Length = BaseHalfSize + 1;
 
                    var res = new GeometryGroup()
                    {
                        FillRule = FillRule.Nonzero
                    };
                    if (level1 == 0)
                    {
                        // 画上面那根线
                        res.Children.Add(new LineGeometry
                        {
                            StartPoint = new Point((level1 * BaseSize) + BaseHalfSize, 0),
                            EndPoint = new Point((level1 * BaseSize) + BaseHalfSize, BaseHalfSize - (Length / 2)),
                        });
                        // 画右边那根线
                        res.Children.Add(new LineGeometry
                        {
                            StartPoint = new Point((level1 * BaseSize) + BaseHalfSize + (Length / 2), BaseHalfSize),
                            EndPoint = new Point((level1 * BaseSize) + BaseSize, BaseHalfSize),
                        });
                    }
                    else
                    {
                        // 画上面那根线
                        res.Children.Add(new LineGeometry
                        {
                            StartPoint = new Point((level1 * BaseSize) + BaseHalfSize, 0),
                            EndPoint = new Point((level1 * BaseSize) + BaseHalfSize, BaseHalfSize - (Length / 2)),
                        });
                        // 画右边那根线
                        res.Children.Add(new LineGeometry
                        {
                            StartPoint = new Point((level1 * BaseSize) + BaseHalfSize + (Length / 2), BaseHalfSize),
                            EndPoint = new Point((level1 * BaseSize) + BaseSize, BaseHalfSize),
                        });
 
                        var parent = tlvi.parent;
                        while (parent != null)
                        {
                            if (!parent.IsLastNode)
                            {
                                res.Children.Add(new LineGeometry
                                {
                                    StartPoint = new Point((parent.level * BaseSize) + BaseHalfSize, 0),
                                    EndPoint = new Point((parent.level * BaseSize) + BaseHalfSize, BaseSize),
                                });
                            }
                            parent = parent.parent;
                        }
                    }
 
                    if (!IsLastNode)
                    {
                        res.Children.Add(new LineGeometry
                        {
                            StartPoint = new Point((level1 * BaseSize) + BaseHalfSize, BaseHalfSize + (Length / 2)),
                            EndPoint = new Point((level1 * BaseSize) + BaseHalfSize, BaseSize),
                        });
                    }
 
 
                    if (HasItems)
                    {
                        //画方
                        res.Children.Add(new RectangleGeometry(new Rect((level1 * BaseSize) + ((BaseSize - Length) / 2), ((BaseSize - Length) / 2), Length, Length)));
 
                        res.Children.Add(new LineGeometry
                        {
                            StartPoint = new Point((level1 * BaseSize) + BaseHalfSize - (Length / 2),
                        BaseHalfSize),
                            EndPoint = new Point((level1 * BaseSize) + BaseHalfSize + (Length / 2), BaseHalfSize),
                        });
 
                        if (!IsExpanded)
                        {
                            res.Children.Add(new LineGeometry
                            {
                                StartPoint = new Point((level1 * BaseSize) + BaseHalfSize,
                            BaseHalfSize - (Length / 2)),
                                EndPoint = new Point((level1 * BaseSize) + BaseHalfSize,
                            BaseHalfSize + (Length / 2)),
                            });
                        }
                    }
                    else
                    {
                        if (!IsLastNode)
                        {
                            res.Children.Add(new LineGeometry
                            {
                                StartPoint = new Point((level1 * BaseSize) + BaseHalfSize, BaseHalfSize - (Length / 2)),
                                EndPoint = new Point((level1 * BaseSize) + BaseHalfSize, BaseHalfSize + (Length / 2)),
                            });
                        }
                        else
                        {
                            res.Children.Add(new LineGeometry
                            {
                                StartPoint = new Point((level1 * BaseSize) + BaseHalfSize, BaseHalfSize - (Length / 2)),
                                EndPoint = new Point((level1 * BaseSize) + BaseHalfSize, BaseHalfSize),
                            });
                        }
 
                        res.Children.Add(new LineGeometry
                        {
                            StartPoint = new Point((level1 * BaseSize) + BaseHalfSize, BaseHalfSize),
                            EndPoint = new Point((level1 * BaseSize) + BaseHalfSize + (Length / 2), BaseHalfSize),
                        });
                    }
                    tlvi.root.UpdateColumnsWidth();
                    return res;
                }
            }
            return null;
        }
 
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
 
    /// <summary>
    /// StepList按钮转换
    /// </summary>
    public class TreeListViewButtonConverter : IMultiValueConverter
    {
        public static TreeListViewButtonConverter Instance = new TreeListViewButtonConverter();
 
 
        public static readonly double sqrt2 = Math.Sqrt(2);
 
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values[0] is TreeListViewItem tlvi)
            {
                if (values[1] is int level && parameter.ToString() == "Size")
                {
                    double BaseSize = (double)values[2];
                    return (level + 1) * BaseSize;
                }
 
                if (parameter.ToString() == "Index")
                {
                    int totalIndex = 1;
                    int Traverse(TreeListViewItem item)
                    {
                        if (item != tlvi)
                        {
                            totalIndex++;
                            foreach (var obj in item.Items)
                            {
                                var childItem = item.ItemContainerGenerator.ContainerFromItem(obj) as TreeListViewItem;
                                if (childItem != null)
                                    return Traverse(childItem);
                            }
                        }
                        return totalIndex;
                    }
 
                    foreach (var obj in tlvi.root.Items)
                    {
                        var item_0 = tlvi.root.ItemContainerGenerator.ContainerFromItem(obj) as TreeListViewItem;
                        if (item_0 != null)
                            return Traverse(item_0);
                    }
                    return totalIndex;
                }
 
 
                if (values[1] is int level1 && values[2] is int index
                    && values[3] is bool IsExpanded
                    && values[4] is bool IsLastNode
                     && values[5] is bool HasItems && parameter.ToString() == "Content")
                {
                    double BaseSize = (double)values[6];
                    double BaseHalfSize = BaseSize / 2;
                    double Radius = BaseHalfSize * 0.6;
                    var res = new GeometryGroup()
                    {
                        FillRule = FillRule.Nonzero
                    };
                    if (level1 == 0)
                    {
                        res.Children.Add(new LineGeometry
                        {
                            StartPoint = new Point((level1 * BaseSize) + BaseHalfSize, 0),
                            EndPoint = new Point((level1 * BaseSize) + BaseHalfSize, BaseHalfSize - Radius),
                        });
                    }
                    else
                    {
                        if (index == 0)
                        {
                            res.Children.Add(new LineGeometry
                            {
                                StartPoint = new Point((level1 * BaseSize), 0),
                                EndPoint = new Point((level1 * BaseSize) + (((sqrt2 * BaseHalfSize) - Radius) / sqrt2),
                                ((sqrt2 * BaseHalfSize) - Radius) / sqrt2)
                            });
                        }
                        else
                        {
                            res.Children.Add(new LineGeometry
                            {
                                StartPoint = new Point((level1 * BaseSize) + BaseHalfSize, 0),
                                EndPoint = new Point((level1 * BaseSize) + BaseHalfSize, BaseHalfSize - Radius),
                            });
                        }
                        var parent = tlvi.parent;
                        while (parent != null)
                        {
                            if (!parent.IsLastNode)
                            {
                                res.Children.Add(new LineGeometry
                                {
                                    StartPoint = new Point((parent.level * BaseSize) + BaseHalfSize, 0),
                                    EndPoint = new Point((parent.level * BaseSize) + BaseHalfSize, BaseSize),
                                });
                            }
                            parent = parent.parent;
                        }
                    }
 
                    if (!IsLastNode)
                    {
                        res.Children.Add(new LineGeometry
                        {
                            StartPoint = new Point((level1 * BaseSize) + BaseHalfSize, BaseHalfSize + Radius),
                            EndPoint = new Point((level1 * BaseSize) + BaseHalfSize, BaseSize),
                        });
                    }
 
                    // 有子项并且是展开状态,统一画斜线
                    if (HasItems)
                    {
                        if (IsExpanded)
                        {
                            res.Children.Add(new LineGeometry
                            {
                                StartPoint = new Point((level1 * BaseSize) + BaseHalfSize + (((sqrt2 * BaseHalfSize) - Radius) / sqrt2),
                                BaseHalfSize + (((sqrt2 * BaseHalfSize) - Radius) / sqrt2)),
                                EndPoint = new Point((level1 + 1) * BaseSize, BaseSize),
                            });
                        }
                    }
 
                    //最后画圆
                    res.Children.Add(new EllipseGeometry
                    {
                        Center = new Point((level1 * BaseSize) + BaseHalfSize, BaseHalfSize),
                        RadiusX = Radius,
                        RadiusY = Radius,
 
                    });
 
                    if (HasItems)
                    {
                        res.Children.Add(new LineGeometry
                        {
                            StartPoint = new Point((level1 * BaseSize) + BaseHalfSize - Radius,
                        BaseHalfSize),
                            EndPoint = new Point((level1 * BaseSize) + BaseHalfSize + Radius, BaseHalfSize),
                        });
 
                        if (!IsExpanded)
                        {
                            res.Children.Add(new LineGeometry
                            {
                                StartPoint = new Point((level1 * BaseSize) + BaseHalfSize,
                            BaseHalfSize - Radius),
                                EndPoint = new Point((level1 * BaseSize) + BaseHalfSize,
                            BaseHalfSize + Radius),
                            });
                        }
                    }
                    tlvi.root.UpdateColumnsWidth();
                    return res;
                }
            }
            return null;
        }
 
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
 
    public class TreeListView : TreeView
    {
        public static readonly DependencyProperty ChildrenPathProperty =
            DependencyProperty.Register("ChildrenPath", typeof(string), typeof(TreeListView),
                new PropertyMetadata(null));
 
        public string ChildrenPath
        {
            get { return (string)GetValue(ChildrenPathProperty); }
            set { SetValue(ChildrenPathProperty, value); }
        }
 
        public static readonly DependencyProperty ColumnsProperty =
            DependencyProperty.Register("Columns", typeof(GridViewColumnCollection), typeof(TreeListView),
                new PropertyMetadata(null));
 
        public GridViewColumnCollection Columns
        {
            get { return (GridViewColumnCollection)GetValue(ColumnsProperty); }
            set { SetValue(ColumnsProperty, value); }
        }
 
        public static readonly DependencyProperty BorderColorProperty =
            DependencyProperty.Register("BorderColor", typeof(Brush), typeof(TreeListView),
                new PropertyMetadata(Brushes.Transparent));
        public Brush BorderColor
        {
            get { return (Brush)GetValue(BorderColorProperty); }
            set { SetValue(BorderColorProperty, value); }
        }
 
        public static readonly DependencyProperty ColumnHeaderContainerStyleProperty =
            DependencyProperty.Register("ColumnHeaderContainerStyle", typeof(Style), typeof(TreeListView),
                new PropertyMetadata(null));
 
        public Style ColumnHeaderContainerStyle
        {
            get { return (Style)GetValue(ColumnHeaderContainerStyleProperty); }
            set { SetValue(ColumnHeaderContainerStyleProperty, value); }
        }
 
        /// <summary>
        /// 鼠标移动颜色
        /// </summary>
        public static readonly DependencyProperty ItemHoverColorProperty =
            DependencyProperty.Register("ItemHoverColor", typeof(Brush), typeof(TreeListView),
                new PropertyMetadata(Brushes.Red));
        public Brush ItemHoverColor
        {
            get { return (Brush)GetValue(ItemHoverColorProperty); }
            set { SetValue(ItemHoverColorProperty, value); }
        }
 
        /// <summary>
        /// 鼠标移动颜色
        /// </summary>
        public static readonly DependencyProperty ItemHoverForegroundProperty =
            DependencyProperty.Register("ItemHoverForeground", typeof(Brush), typeof(TreeListView),
                new PropertyMetadata(SystemColors.HighlightTextBrush));
        public Brush ItemHoverForeground
        {
            get { return (Brush)GetValue(ItemHoverForegroundProperty); }
            set { SetValue(ItemHoverForegroundProperty, value); }
        }
 
        /// <summary>
        /// Item选中颜色
        /// </summary>
        public static readonly DependencyProperty ItemSelectedColorProperty =
            DependencyProperty.Register("ItemSelected", typeof(Brush), typeof(TreeListView),
                new PropertyMetadata(SystemColors.HighlightBrush));
        public Brush ItemSelectedColor
        {
            get { return (Brush)GetValue(ItemSelectedColorProperty); }
            set { SetValue(ItemSelectedColorProperty, value); }
        }
 
        /// <summary>
        /// Item选中字体颜色
        /// </summary>
        public static readonly DependencyProperty ItemSelectedForegroundProperty =
            DependencyProperty.Register("ItemSelectedForeground", typeof(Brush), typeof(TreeListView),
                new PropertyMetadata(SystemColors.HighlightTextBrush));
        public Brush ItemSelectedForeground
        {
            get { return (Brush)GetValue(ItemSelectedForegroundProperty); }
            set { SetValue(ItemSelectedForegroundProperty, value); }
        }
 
        static TreeListView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TreeListView), new FrameworkPropertyMetadata(typeof(TreeListView)));
        }
 
        public TreeListView()
        {
            // this.SizeChanged += TreeListView_SizeChanged;
            Columns = new GridViewColumnCollection();
        }
 
        private void TreeListView_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            double oldWidth = e.PreviousSize.Width, newWidth = e.NewSize.Width;
            double changed = newWidth - oldWidth;
            int columns = 0;
            if (oldWidth <= 0)
            {
                foreach (GridViewColumn col in Columns)
                {
                    GridLength? length = TreeViewColumnWidthHelper.GetAutoWidth(col);
                    if (length == null)
                    {
                        changed -= col.Width;
                        continue;
                    }
                    if (length.Value.GridUnitType == GridUnitType.Auto)
                    {
                        if (double.IsNaN(col.Width))
                        {
                            col.Width = col.ActualWidth;
                        }
                        col.Width = double.NaN;
                        changed -= col.ActualWidth;
                    }
                    else if (length.Value.GridUnitType == GridUnitType.Star)
                    {
                        columns += 1;
                    }
                }
 
                foreach (GridViewColumn col in Columns)
                {
                    GridLength? length = TreeViewColumnWidthHelper.GetAutoWidth(col);
                    if (length != null && length.Value.GridUnitType == GridUnitType.Star)
                    {
                        col.Width = Math.Max(20, col.Width + (changed / columns));
                    }
                }
            }
            else
            {
                foreach (GridViewColumn col in Columns)
                {
                    GridLength? length = TreeViewColumnWidthHelper.GetAutoWidth(col);
                    if (length != null && length.Value.GridUnitType == GridUnitType.Star)
                    {
                        columns += 1;
                    }
                }
                if (columns <= 0)
                {
                    return;
                }
 
                foreach (GridViewColumn col in Columns)
                {
                    GridLength? length = TreeViewColumnWidthHelper.GetAutoWidth(col);
                    if (length != null && length.Value.GridUnitType == GridUnitType.Star)
                    {
                        col.Width = Math.Max(20, col.Width + (changed / columns));
                    }
                }
            }
        }
 
        protected override void OnItemTemplateChanged(DataTemplate oldItemTemplate, DataTemplate newItemTemplate)
        {
            try
            {
                HierarchicalDataTemplate ttt = newItemTemplate as HierarchicalDataTemplate;
                if (ttt != null)
                {
                    ttt.ItemsSource = new Binding(ChildrenPath)
                    {
                        Mode = BindingMode.OneWay
                    };
                }
            }
            catch
            {
 
            }
            base.OnItemTemplateChanged(oldItemTemplate, newItemTemplate);
        }
 
        internal void UpdateColumnsWidth()
        {
            int i = 0;
            var totalWidth = this.ActualWidth - 20;
            int columns = 0;
            // foreach(var item in this.It)
 
            foreach (GridViewColumn col in Columns)
            {
                GridLength? length = TreeViewColumnWidthHelper.GetAutoWidth(col);
                if (length == null || length.Value.GridUnitType == GridUnitType.Pixel)
                {
                    col.Width = col.Width;
                    totalWidth -= col.Width;
                }
                else if (length.Value.GridUnitType == GridUnitType.Auto)
                {
                    if (double.IsNaN(col.Width))
                    {
                        col.Width = col.ActualWidth;
                    }
                    col.Width = double.NaN;
                    totalWidth -= col.ActualWidth;
                }
                else if (length.Value.GridUnitType == GridUnitType.Star)
                {
                    columns += 1;
                }
            }
 
            //foreach (GridViewColumn col in Columns)
            //{
            //    GridLength? length = TreeViewColumnWidthHelper.GetAutoWidth(col);
            //    if (length != null && length.Value.GridUnitType == GridUnitType.Star)
            //    {
            //        col.Width = totalWidth / columns;
            //    }
            //}
        }
 
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }
 
        protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);
        }
 
        protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
        {
            base.OnItemsSourceChanged(oldValue, newValue);
            if (oldValue is INotifyCollectionChanged oncc)
            {
                oncc.CollectionChanged -= Nncc_CollectionChanged;
            }
 
            if (oldValue is INotifyPropertyChanged onpc)
            {
                onpc.PropertyChanged -= Nnpc_PropertyChanged;
            }
 
 
            if (newValue is INotifyCollectionChanged nncc)
            {
                nncc.CollectionChanged += Nncc_CollectionChanged;
            }
 
            if (newValue is INotifyPropertyChanged nnpc)
            {
                nnpc.PropertyChanged += Nnpc_PropertyChanged;
            }
        }
 
        internal void UpdateChildrenUI()
        {
            if (ItemsSource is IEnumerable source)
            {
                foreach (var obj in source)
                {
                    var sb = this.ItemContainerGenerator.ContainerFromItem(obj) as TreeListViewItem;
                    sb?.TryUpdateUI();
                }
            }
        }
 
        private void Nnpc_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            //UpdateChildrenUI();
        }
 
        private void Nncc_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            UpdateChildrenUI();
        }
 
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new TreeListViewItem(0, this, null);
        }
    }
 
    public class TreeListViewItem : TreeViewItem, INotifyPropertyChanged
    {
        public static void TreeViewItem_PreviewMouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // 向上遍历视觉树,查找是否有需要处理双击的控件
            DependencyObject current = e.OriginalSource as DependencyObject;
            while (current != null)
            {
                // 如果找到需要处理双击的控件(例如Button、TextBox等),则不阻止事件
                if (current is EditBox eb && eb.CanEdit)
                {
                    return; // 让事件继续传递
                }
                current = VisualTreeHelper.GetParent(current);
            }
            e.Handled = true;
        }
 
        public static readonly DependencyProperty ColumnsProperty =
            DependencyProperty.Register("Columns", typeof(GridViewColumnCollection), typeof(TreeListViewItem),
                new PropertyMetadata(null));
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        private int _totalIndex;
        public int TotalIndex
        {
            get => _totalIndex;
            set
            {
                if (_totalIndex != value)
                {
                    _totalIndex = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TotalIndex)));
                }
            }
        }
 
        public int level
        {
            get
            {
                int i = 0;
                var p = parent;
                while (p != null)
                {
                    i++;
                    p = p.parent;
                }
                return i;
            }
        }
 
        public int index
        {
            get
            {
                return parent == null ? root.Items.IndexOf(this.DataContext) : parent.Items.IndexOf(this.DataContext);
            }
        }
 
        public bool IsLastNode
        {
            get
            {
                return parent == null ? index == root.Items.Count - 1 : index == parent.Items.Count - 1;
            }
        }
 
        public TreeListView root { get; private set; }
        public TreeListViewItem parent { get; private set; }
 
        public TreeListViewItem(int level, TreeListView root, TreeListViewItem parent)
        {
            this.IsExpanded = true;
            this.root = root;
            this.parent = parent;
            this.Loaded += TreeListViewItem_Loaded;
            this.AddHandler(TreeViewItem.RequestBringIntoViewEvent,
            new RequestBringIntoViewEventHandler(TreeViewItem_RequestBringIntoView),
            handledEventsToo: true);
        }
 
        private void TreeViewItem_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
        {
            // 取消自动滚动
            e.Handled = true;
        }
 
        private void TreeListViewItem_Loaded(object sender, RoutedEventArgs e)
        {
 
        }
 
        protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
        {
            base.OnItemsSourceChanged(oldValue, newValue);
            if (oldValue is INotifyCollectionChanged oncc)
            {
                oncc.CollectionChanged -= Nncc_CollectionChanged;
            }
 
            if (oldValue is INotifyPropertyChanged onpc)
            {
                onpc.PropertyChanged -= Nnpc_PropertyChanged;
            }
 
 
            if (newValue is INotifyCollectionChanged nncc)
            {
                nncc.CollectionChanged += Nncc_CollectionChanged;
            }
 
            if (newValue is INotifyPropertyChanged nnpc)
            {
                nnpc.PropertyChanged += Nnpc_PropertyChanged;
            }
        }
 
        protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);
        }
 
        private void Nnpc_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            //root.UpdateChildrenUI();
        }
 
        private void Nncc_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            //root.UpdateChildrenUI();
            TryUpdateUI();
        }
 
        internal void TryUpdateUI()
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(level)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(index)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsLastNode)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(HasItems)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsExpanded)));
            if (ItemsSource is IEnumerable source)
            {
                foreach (var obj in source)
                {
                    var sb = this.ItemContainerGenerator.ContainerFromItem(obj) as TreeListViewItem;
                    sb?.TryUpdateUI();
                }
            }
        }
 
        static TreeListViewItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TreeListViewItem), new FrameworkPropertyMetadata(typeof(TreeListViewItem)));
        }
 
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new TreeListViewItem(level + 1, root, this);
        }
    }
}