From e12c419ef3ef90244b9dfa9bec54ba4d307d7aeb Mon Sep 17 00:00:00 2001 From: chfei Date: Wed, 9 Sep 2026 15:49:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Main.Test/DataGridEx.cs | 13 ++++ Main.Test/DataGridHelper.cs | 133 ++++++++++++++++++++++++++++++++++- Main.Test/Dictionary1.xaml | 91 +++++++++++++++++++++--- Main.Test/MainWindow.xaml | 5 +- Main.Test/MainWindow.xaml.cs | 4 +- 5 files changed, 230 insertions(+), 16 deletions(-) create mode 100644 Main.Test/DataGridEx.cs diff --git a/Main.Test/DataGridEx.cs b/Main.Test/DataGridEx.cs new file mode 100644 index 0000000..437effa --- /dev/null +++ b/Main.Test/DataGridEx.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Controls; + +namespace Main.Test; + +public class DataGridEx : DataGrid +{ + +} diff --git a/Main.Test/DataGridHelper.cs b/Main.Test/DataGridHelper.cs index 2cab66c..88d55d2 100644 --- a/Main.Test/DataGridHelper.cs +++ b/Main.Test/DataGridHelper.cs @@ -1,9 +1,14 @@ -using System; +using HandyControl.Controls; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; +using System.Windows.Controls; +using System.Windows.Controls.Primitives; +using System.Windows.Documents; +using System.Windows.Media; namespace Main.Test; @@ -55,4 +60,130 @@ public class DataGridHelper public static readonly DependencyProperty IsActiveProperty = DependencyProperty.RegisterAttached("IsActive", typeof(bool), typeof(DataGridHelper)); + + + public static DependencyProperty ShowRowNumberProperty = DependencyProperty.RegisterAttached("ShowRowNumber", + typeof(bool), typeof(DataGridAttach), + new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, OnShowRowNumberChanged)); + + public static bool GetShowRowNumber(DependencyObject target) => (bool)target.GetValue(ShowRowNumberProperty); + + public static void SetShowRowNumber(DependencyObject target, bool value) => target.SetValue(ShowRowNumberProperty, value); + + private static void OnShowRowNumberChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) + { + if (target is not DataGrid dataGrid) return; + var show = (bool)e.NewValue; + + if (show) + { + dataGrid.LoadingRow += DataGrid_LoadingRow; + dataGrid.ItemContainerGenerator.ItemsChanged += ItemContainerGeneratorItemsChanged; + } + else + { + dataGrid.LoadingRow -= DataGrid_LoadingRow; + dataGrid.ItemContainerGenerator.ItemsChanged -= ItemContainerGeneratorItemsChanged; + } + + + + void ItemContainerGeneratorItemsChanged(object sender, ItemsChangedEventArgs e) + { + var generator = dataGrid.ItemContainerGenerator; + var itemsCount = dataGrid.Items.Count; + + if (show) + { + for (var i = 0; i < itemsCount; i++) + { + var row = (DataGridRow)generator.ContainerFromIndex(i); + if (row != null) + { + row.Header = (i + 1).ToString(); + } + } + } + else + { + for (var i = 0; i < itemsCount; i++) + { + var row = (DataGridRow)generator.ContainerFromIndex(i); + if (row != null) + { + row.Header = null; + } + } + } + } + } + + private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) => e.Row.Header = (e.Row.GetIndex() + 1).ToString(); + + + + + public static readonly DependencyProperty ShowBorderProperty = + DependencyProperty.RegisterAttached("ShowBorder", typeof(bool), + typeof(DataGridHelper), new PropertyMetadata(false, OnShowBorderChanged)); + + public static void SetShowBorder(DependencyObject element, bool value) + => element.SetValue(ShowBorderProperty, value); + + public static bool GetShowBorder(DependencyObject element) + => (bool)element.GetValue(ShowBorderProperty); + + private static void OnShowBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is DataGridColumnHeader header && (bool)e.NewValue) + { + header.Loaded += (s, args) => + { + var layer = AdornerLayer.GetAdornerLayer(header); + if (layer != null) + { + layer.Add(new BorderAdorner(header)); + } + }; + } + } + } +public class BorderAdorner : Adorner +{ + public BorderAdorner(UIElement adornedElement) : base(adornedElement) { } + + protected override void OnRender(DrawingContext drawingContext) + { + var header = AdornedElement as DataGridColumnHeader; + if (header == null) return; + + var pen = new Pen(header.BorderBrush ?? new SolidColorBrush(Colors.LightGray), 1); + + // 绘制右侧竖线(永远不消失) + drawingContext.DrawLine(pen, + new Point(header.ActualWidth - 0.5, 0), + new Point(header.ActualWidth - 0.5, header.ActualHeight)); + } +} +// 先创建自定义类(如果还没创建) +public class CustomDataGridColumnHeader : DataGridColumnHeader +{ + protected override void OnRender(DrawingContext drawingContext) + { + base.OnRender(drawingContext); + + // 在这里绘制边框,永远不消失 + var pen = new Pen(new SolidColorBrush(Colors.LightGray), 1); + + // 绘制右侧竖线 + drawingContext.DrawLine(pen, + new Point(ActualWidth - 0.5, 0), + new Point(ActualWidth - 0.5, ActualHeight)); + + // 绘制底部横线(如果需要) + // drawingContext.DrawLine(pen, + // new Point(0, ActualHeight - 0.5), + // new Point(ActualWidth, ActualHeight - 0.5)); + } +} \ No newline at end of file diff --git a/Main.Test/Dictionary1.xaml b/Main.Test/Dictionary1.xaml index 690384f..42078cb 100644 --- a/Main.Test/Dictionary1.xaml +++ b/Main.Test/Dictionary1.xaml @@ -11,10 +11,13 @@ + + + FocusVisualStyle="{x:Null}"> + + + + @@ -137,9 +175,25 @@