优化datagrid

This commit is contained in:
chfei committed 2026-10-21 17:11:24 +08:00
1 parent e12c419ef3
commit 233ec5d58d
8 files changed
+429 -57

No files matched your search

+9 -25
View File
@@ -63,12 +63,15 @@ public class DataGridHelper
public static DependencyProperty ShowRowNumberProperty = DependencyProperty.RegisterAttached("ShowRowNumber",
typeof(bool), typeof(DataGridAttach),
typeof(bool), typeof(DataGridHelper),
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);
public static void SetShowRowNumber(DependencyObject target, bool value)
{
target.SetValue(ShowRowNumberProperty, value);
}
private static void OnShowRowNumberChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
@@ -118,7 +121,10 @@ public class DataGridHelper
}
}
private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) => e.Row.Header = (e.Row.GetIndex() + 1).ToString();
private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = (e.Row.GetIndex() + 1).ToString();
}
@@ -126,7 +132,6 @@ public class DataGridHelper
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);
@@ -166,24 +171,3 @@ public class BorderAdorner : Adorner
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));
}
}