提交
This commit is contained in:
1 parent
11599bc7d3
commit
e12c419ef3
5 files changed
+230
-16
No files matched your search
@@ -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
|
||||
{
|
||||
|
||||
}
|
||||
+132
-1
@@ -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));
|
||||
}
|
||||
}
|
||||
+80
-11
@@ -11,10 +11,13 @@
|
||||
|
||||
|
||||
<Style x:Key="DefaultCellHeaderStyle" TargetType="DataGridColumnHeader">
|
||||
<Setter Property="UseLayoutRounding" Value="True" />
|
||||
<Setter Property="ext:DataGridHelper.ShowBorder" Value="True" />
|
||||
|
||||
<Setter Property="Height" Value="{Binding ColumnHeaderHeight, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
|
||||
<Setter Property="Background" Value="#eeeeee" />
|
||||
<Setter Property="BorderThickness" Value="1,0,1,1" />
|
||||
<Setter Property="Margin" Value="-1,0,0,0" />
|
||||
<Setter Property="BorderThickness" Value="0,0,0,1" />
|
||||
<!-- 只保留底边框,竖线不再用 Border 画 -->
|
||||
<Setter Property="HorizontalContentAlignment" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=HorizontalContentAlignment}" />
|
||||
<Setter Property="Padding" Value="8,0" />
|
||||
<Setter Property="FontSize" Value="{DynamicResource DefaultFontSize}" />
|
||||
@@ -28,7 +31,8 @@
|
||||
Padding="{TemplateBinding Padding}"
|
||||
Background="{TemplateBinding Background}"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}">
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
UseLayoutRounding="True">
|
||||
<Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
|
||||
<ContentPresenter
|
||||
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
|
||||
@@ -63,18 +67,52 @@
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Border>
|
||||
|
||||
<!-- 关键改动:用固定宽度的 Rectangle 画右侧竖线,永不消失 -->
|
||||
<Rectangle
|
||||
Width="1"
|
||||
HorizontalAlignment="Right"
|
||||
Fill="{TemplateBinding BorderBrush}"
|
||||
SnapsToDevicePixels="False" />
|
||||
|
||||
<Thumb
|
||||
x:Name="PART_LeftHeaderGripper"
|
||||
Width="6"
|
||||
HorizontalAlignment="Left"
|
||||
Background="Transparent"
|
||||
Cursor="SizeWE"
|
||||
FocusVisualStyle="{x:Null}"
|
||||
Opacity="0" />
|
||||
FocusVisualStyle="{x:Null}">
|
||||
<Thumb.Style>
|
||||
<Style TargetType="Thumb">
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="Thumb">
|
||||
<Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" />
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
</Thumb.Style>
|
||||
</Thumb>
|
||||
<Thumb
|
||||
x:Name="PART_RightHeaderGripper"
|
||||
Width="6"
|
||||
HorizontalAlignment="Right"
|
||||
Background="Transparent"
|
||||
Cursor="SizeWE"
|
||||
FocusVisualStyle="{x:Null}"
|
||||
Opacity="0" />
|
||||
FocusVisualStyle="{x:Null}">
|
||||
<Thumb.Style>
|
||||
<Style TargetType="Thumb">
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="Thumb">
|
||||
<Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" />
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
</Thumb.Style>
|
||||
</Thumb>
|
||||
</Grid>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="SortDirection" Value="Ascending">
|
||||
@@ -137,9 +175,25 @@
|
||||
</Style>
|
||||
|
||||
<Style x:Key="DefaultRowHeaderStyle" TargetType="{x:Type DataGridRowHeader}">
|
||||
<Setter Property="BorderThickness" Value="0" />
|
||||
<Setter Property="BorderBrush" Value="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
|
||||
<Setter Property="BorderThickness" Value="0,0,1,1" />
|
||||
<Setter Property="FontSize" Value="{DynamicResource DefaultFontSize}" />
|
||||
<Setter Property="FontFamily" Value="{DynamicResource FontFamily}" />
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="{x:Type DataGridRowHeader}">
|
||||
<Border
|
||||
Background="Transparent"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}">
|
||||
<ContentPresenter
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
TextElement.FontSize="10" />
|
||||
</Border>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
|
||||
<Style x:Key="DefaultRowStyle" TargetType="{x:Type DataGridRow}">
|
||||
@@ -184,7 +238,8 @@
|
||||
<Grid
|
||||
x:Name="fousIcon"
|
||||
IsHitTestVisible="False"
|
||||
Opacity="0">
|
||||
Opacity="0"
|
||||
Visibility="Collapsed">
|
||||
<Path
|
||||
Width="10"
|
||||
Height="10"
|
||||
@@ -290,6 +345,8 @@
|
||||
</Style>
|
||||
|
||||
<Style x:Key="DefaultDataGridStyle" TargetType="DataGrid">
|
||||
<Setter Property="UseLayoutRounding" Value="True" />
|
||||
<Setter Property="SnapsToDevicePixels" Value="True" />
|
||||
<Setter Property="Background" Value="White" />
|
||||
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
|
||||
<Setter Property="ColumnHeaderHeight" Value="36" />
|
||||
@@ -325,7 +382,8 @@
|
||||
Background="{TemplateBinding Background}"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
SnapsToDevicePixels="True">
|
||||
SnapsToDevicePixels="True"
|
||||
UseLayoutRounding="True">
|
||||
<ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
|
||||
<ScrollViewer.Template>
|
||||
<ControlTemplate TargetType="{x:Type ScrollViewer}">
|
||||
@@ -340,13 +398,24 @@
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<Border
|
||||
Grid.Row="0"
|
||||
Grid.Column="0"
|
||||
Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
|
||||
Background="#eeeeee"
|
||||
BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
|
||||
BorderThickness="1,0,1,1" />
|
||||
|
||||
<DataGridColumnHeadersPresenter
|
||||
x:Name="PART_ColumnHeadersPresenter"
|
||||
Grid.Column="1"
|
||||
Grid.ColumnSpan="2"
|
||||
Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
|
||||
|
||||
<Grid Grid.Row="1" Grid.Column="1">
|
||||
<Grid
|
||||
Grid.Row="1"
|
||||
Grid.Column="0"
|
||||
Grid.ColumnSpan="2">
|
||||
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" />
|
||||
<Grid
|
||||
x:Name="NoDataTipsGrid"
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:hc="https://handyorg.github.io/handycontrol"
|
||||
xmlns:local="clr-namespace:Main.Test"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:tf="Trade.Framework"
|
||||
@@ -12,8 +13,6 @@
|
||||
Width="800"
|
||||
Height="450"
|
||||
DataContext="{tf:DISource Type=vm:MainViewModel}"
|
||||
NonClientAreaContent=""
|
||||
NonClientAreaHeight="200"
|
||||
ResizeMode="CanResizeWithGrip"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
mc:Ignorable="d">
|
||||
@@ -22,7 +21,9 @@
|
||||
<DataGrid
|
||||
Height="Auto"
|
||||
d:ItemsSource="{d:SampleData ItemCount=10}"
|
||||
hc:DataGridAttach.ShowRowNumber="True"
|
||||
ItemsSource="{Binding Data}"
|
||||
RowHeaderWidth="30"
|
||||
Style="{StaticResource DefaultDataGridStyle}">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn
|
||||
|
||||
@@ -21,8 +21,8 @@ public partial class MainWindow : WindowBase
|
||||
{
|
||||
InitializeComponent();
|
||||
var mainHeaderImageContent = new MainHeaderImageContent();
|
||||
NonClientAreaHeight = mainHeaderImageContent.Height;
|
||||
NonClientAreaContent = mainHeaderImageContent;
|
||||
//NonClientAreaHeight = mainHeaderImageContent.Height;
|
||||
//NonClientAreaContent = mainHeaderImageContent;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user