优化datagrid
This commit is contained in:
1 parent
e12c419ef3
commit
233ec5d58d
8 files changed
+429
-57
No files matched your search
+1
-1
@@ -11,7 +11,7 @@
|
|||||||
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Basic.xaml" />
|
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Basic.xaml" />
|
||||||
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml" />
|
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml" />
|
||||||
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml" />
|
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml" />
|
||||||
<ResourceDictionary Source="Dictionary1.xaml" />
|
<ResourceDictionary Source="Themes/Generic.xaml" />
|
||||||
<ResourceDictionary Source="Dictionary2.xaml" />
|
<ResourceDictionary Source="Dictionary2.xaml" />
|
||||||
</ResourceDictionary.MergedDictionaries>
|
</ResourceDictionary.MergedDictionaries>
|
||||||
</ResourceDictionary>
|
</ResourceDictionary>
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
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
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -63,12 +63,15 @@ public class DataGridHelper
|
|||||||
|
|
||||||
|
|
||||||
public static DependencyProperty ShowRowNumberProperty = DependencyProperty.RegisterAttached("ShowRowNumber",
|
public static DependencyProperty ShowRowNumberProperty = DependencyProperty.RegisterAttached("ShowRowNumber",
|
||||||
typeof(bool), typeof(DataGridAttach),
|
typeof(bool), typeof(DataGridHelper),
|
||||||
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, OnShowRowNumberChanged));
|
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, OnShowRowNumberChanged));
|
||||||
|
|
||||||
public static bool GetShowRowNumber(DependencyObject target) => (bool)target.GetValue(ShowRowNumberProperty);
|
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)
|
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 =
|
public static readonly DependencyProperty ShowBorderProperty =
|
||||||
DependencyProperty.RegisterAttached("ShowBorder", typeof(bool),
|
DependencyProperty.RegisterAttached("ShowBorder", typeof(bool),
|
||||||
typeof(DataGridHelper), new PropertyMetadata(false, OnShowBorderChanged));
|
typeof(DataGridHelper), new PropertyMetadata(false, OnShowBorderChanged));
|
||||||
|
|
||||||
public static void SetShowBorder(DependencyObject element, bool value)
|
public static void SetShowBorder(DependencyObject element, bool value)
|
||||||
=> element.SetValue(ShowBorderProperty, value);
|
=> element.SetValue(ShowBorderProperty, value);
|
||||||
|
|
||||||
@@ -166,24 +171,3 @@ public class BorderAdorner : Adorner
|
|||||||
new Point(header.ActualWidth - 0.5, header.ActualHeight));
|
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,16 +3,10 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:ext="clr-namespace:Main.Test"
|
xmlns:ext="clr-namespace:Main.Test"
|
||||||
xmlns:sys="clr-namespace:System;assembly=mscorlib">
|
xmlns:sys="clr-namespace:System;assembly=mscorlib">
|
||||||
<!--<FontFamily x:Key="FontFamily">Microsoft YaHei</FontFamily>
|
|
||||||
-->
|
|
||||||
<!-- 默认字体大小 -->
|
|
||||||
<!--
|
|
||||||
<sys:Double x:Key="DefaultFontSize">14</sys:Double>-->
|
|
||||||
|
|
||||||
|
|
||||||
<Style x:Key="DefaultCellHeaderStyle" TargetType="DataGridColumnHeader">
|
<Style x:Key="DefaultCellHeaderStyle" TargetType="DataGridColumnHeader">
|
||||||
<Setter Property="UseLayoutRounding" Value="True" />
|
<Setter Property="UseLayoutRounding" Value="True" />
|
||||||
<Setter Property="ext:DataGridHelper.ShowBorder" Value="True" />
|
<!--<Setter Property="ext:DataGridHelper.ShowBorder" Value="True" />-->
|
||||||
|
|
||||||
<Setter Property="Height" Value="{Binding ColumnHeaderHeight, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
|
<Setter Property="Height" Value="{Binding ColumnHeaderHeight, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
|
||||||
<Setter Property="Background" Value="#eeeeee" />
|
<Setter Property="Background" Value="#eeeeee" />
|
||||||
@@ -165,7 +159,6 @@
|
|||||||
</Grid>
|
</Grid>
|
||||||
<ControlTemplate.Triggers>
|
<ControlTemplate.Triggers>
|
||||||
<Trigger Property="IsSelected" Value="True">
|
<Trigger Property="IsSelected" Value="True">
|
||||||
<!-- 选中后颜色改变 -->
|
|
||||||
<Setter Property="Foreground" Value="White" />
|
<Setter Property="Foreground" Value="White" />
|
||||||
</Trigger>
|
</Trigger>
|
||||||
</ControlTemplate.Triggers>
|
</ControlTemplate.Triggers>
|
||||||
@@ -191,6 +184,11 @@
|
|||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
TextElement.FontSize="10" />
|
TextElement.FontSize="10" />
|
||||||
</Border>
|
</Border>
|
||||||
|
<ControlTemplate.Triggers>
|
||||||
|
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="True">
|
||||||
|
<Setter Property="Foreground" Value="White" />
|
||||||
|
</DataTrigger>
|
||||||
|
</ControlTemplate.Triggers>
|
||||||
</ControlTemplate>
|
</ControlTemplate>
|
||||||
</Setter.Value>
|
</Setter.Value>
|
||||||
</Setter>
|
</Setter>
|
||||||
@@ -404,7 +402,28 @@
|
|||||||
Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
|
Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
|
||||||
Background="#eeeeee"
|
Background="#eeeeee"
|
||||||
BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
|
BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
|
||||||
BorderThickness="1,0,1,1" />
|
BorderThickness="1,0,1,1">
|
||||||
|
<CheckBox
|
||||||
|
Width="32"
|
||||||
|
HorizontalAlignment="Center"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
HorizontalContentAlignment="Center"
|
||||||
|
AutomationProperties.Name="全选行"
|
||||||
|
Command="{x:Static ext:StyledDataGrid.ToggleSelectAllCommand}"
|
||||||
|
CommandTarget="{Binding RelativeSource={RelativeSource AncestorType={x:Type ext:StyledDataGrid}}}"
|
||||||
|
IsChecked="{Binding AreAllRowsSelected, RelativeSource={RelativeSource AncestorType={x:Type ext:StyledDataGrid}}, Mode=OneWay}">
|
||||||
|
<CheckBox.Style>
|
||||||
|
<Style BasedOn="{StaticResource {x:Type CheckBox}}" TargetType="CheckBox">
|
||||||
|
<Setter Property="Visibility" Value="Collapsed" />
|
||||||
|
<Style.Triggers>
|
||||||
|
<DataTrigger Binding="{Binding ShowCheckBox, RelativeSource={RelativeSource AncestorType={x:Type ext:StyledDataGrid}}}" Value="True">
|
||||||
|
<Setter Property="Visibility" Value="Visible" />
|
||||||
|
</DataTrigger>
|
||||||
|
</Style.Triggers>
|
||||||
|
</Style>
|
||||||
|
</CheckBox.Style>
|
||||||
|
</CheckBox>
|
||||||
|
</Border>
|
||||||
|
|
||||||
<DataGridColumnHeadersPresenter
|
<DataGridColumnHeadersPresenter
|
||||||
x:Name="PART_ColumnHeadersPresenter"
|
x:Name="PART_ColumnHeadersPresenter"
|
||||||
|
|||||||
@@ -18,14 +18,17 @@
|
|||||||
mc:Ignorable="d">
|
mc:Ignorable="d">
|
||||||
|
|
||||||
<Grid Margin="0,0,0,0">
|
<Grid Margin="0,0,0,0">
|
||||||
<DataGrid
|
<local:StyledDataGrid
|
||||||
Height="Auto"
|
Height="Auto"
|
||||||
d:ItemsSource="{d:SampleData ItemCount=10}"
|
d:ItemsSource="{d:SampleData ItemCount=10}"
|
||||||
hc:DataGridAttach.ShowRowNumber="True"
|
local:DataGridHelper.ShowBorder="False"
|
||||||
|
GridLinesVisibility="All"
|
||||||
ItemsSource="{Binding Data}"
|
ItemsSource="{Binding Data}"
|
||||||
RowHeaderWidth="30"
|
SelectedRowForeground="White"
|
||||||
Style="{StaticResource DefaultDataGridStyle}">
|
SelectionMode="Extended"
|
||||||
<DataGrid.Columns>
|
ShowCheckBox="True"
|
||||||
|
ShowRowNumber="True">
|
||||||
|
<local:StyledDataGrid.Columns>
|
||||||
<DataGridTextColumn
|
<DataGridTextColumn
|
||||||
Width="1*"
|
Width="1*"
|
||||||
MinWidth="100"
|
MinWidth="100"
|
||||||
@@ -48,11 +51,11 @@
|
|||||||
Width="1*"
|
Width="1*"
|
||||||
MinWidth="100"
|
MinWidth="100"
|
||||||
Binding="{Binding Qty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
|
Binding="{Binding Qty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
|
||||||
CellStyle="{DynamicResource DefaultNumberCellStyle}"
|
CellStyle="{DynamicResource StyledNumberCellStyle}"
|
||||||
Header="数字"
|
Header="数字"
|
||||||
HeaderStyle="{DynamicResource DefaultNumberCellHeaderStyle}"
|
HeaderStyle="{DynamicResource DefaultNumberCellHeaderStyle}"
|
||||||
IsReadOnly="True" />
|
IsReadOnly="True" />
|
||||||
</DataGrid.Columns>
|
</local:StyledDataGrid.Columns>
|
||||||
</DataGrid>
|
</local:StyledDataGrid>
|
||||||
</Grid>
|
</Grid>
|
||||||
</tf:WindowBase>
|
</tf:WindowBase>
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
using System.Collections.Specialized;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Controls;
|
||||||
|
using System.Windows.Input;
|
||||||
|
using System.Windows.Media;
|
||||||
|
using System.Windows.Threading;
|
||||||
|
|
||||||
|
namespace Main.Test;
|
||||||
|
|
||||||
|
/// <summary>带默认样式、行号和行选择勾选框的 DataGrid。</summary>
|
||||||
|
public class StyledDataGrid : DataGrid
|
||||||
|
{
|
||||||
|
public static readonly RoutedUICommand ToggleSelectAllCommand = new("全选或取消全选", nameof(ToggleSelectAllCommand), typeof(StyledDataGrid));
|
||||||
|
private static readonly DependencyPropertyKey AreAllRowsSelectedPropertyKey = DependencyProperty.RegisterReadOnly(
|
||||||
|
nameof(AreAllRowsSelected), typeof(bool?), typeof(StyledDataGrid), new PropertyMetadata(false));
|
||||||
|
public static readonly DependencyProperty AreAllRowsSelectedProperty = AreAllRowsSelectedPropertyKey.DependencyProperty;
|
||||||
|
/// <summary>全部选中为 true,部分选中为 null,未选中为 false。</summary>
|
||||||
|
public bool? AreAllRowsSelected => (bool?)GetValue(AreAllRowsSelectedProperty);
|
||||||
|
public static readonly DependencyProperty SelectedRowBackgroundProperty = DependencyProperty.Register(
|
||||||
|
nameof(SelectedRowBackground), typeof(Brush), typeof(StyledDataGrid),
|
||||||
|
new PropertyMetadata(new SolidColorBrush(Color.FromRgb(75, 178, 255))));
|
||||||
|
public static readonly DependencyProperty SelectedRowForegroundProperty = DependencyProperty.Register(
|
||||||
|
nameof(SelectedRowForeground), typeof(Brush), typeof(StyledDataGrid), new PropertyMetadata(Brushes.White));
|
||||||
|
public static readonly DependencyProperty ShowCheckBoxProperty = DependencyProperty.Register(
|
||||||
|
nameof(ShowCheckBox), typeof(bool), typeof(StyledDataGrid), new PropertyMetadata(false, HeaderOptionsChanged));
|
||||||
|
public static readonly DependencyProperty ShowRowNumberProperty = DependencyProperty.Register(
|
||||||
|
nameof(ShowRowNumber), typeof(bool), typeof(StyledDataGrid), new PropertyMetadata(false, HeaderOptionsChanged));
|
||||||
|
|
||||||
|
public Brush SelectedRowBackground { get => (Brush)GetValue(SelectedRowBackgroundProperty); set => SetValue(SelectedRowBackgroundProperty, value); }
|
||||||
|
public Brush SelectedRowForeground { get => (Brush)GetValue(SelectedRowForegroundProperty); set => SetValue(SelectedRowForegroundProperty, value); }
|
||||||
|
/// <summary>勾选状态与行 IsSelected 同步,可通过 SelectedItems 获取。</summary>
|
||||||
|
public bool ShowCheckBox { get => (bool)GetValue(ShowCheckBoxProperty); set => SetValue(ShowCheckBoxProperty, value); }
|
||||||
|
public bool ShowRowNumber { get => (bool)GetValue(ShowRowNumberProperty); set => SetValue(ShowRowNumberProperty, value); }
|
||||||
|
|
||||||
|
static StyledDataGrid()
|
||||||
|
{
|
||||||
|
DefaultStyleKeyProperty.OverrideMetadata(typeof(StyledDataGrid), new FrameworkPropertyMetadata(typeof(StyledDataGrid)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public StyledDataGrid()
|
||||||
|
{
|
||||||
|
CommandBindings.Add(new CommandBinding(ToggleSelectAllCommand, (_, e) =>
|
||||||
|
{
|
||||||
|
if (AreAllRowsSelected == true) UnselectAll();
|
||||||
|
else SelectAll();
|
||||||
|
e.Handled = true;
|
||||||
|
}, (_, e) =>
|
||||||
|
{
|
||||||
|
e.CanExecute = SelectionMode == DataGridSelectionMode.Extended &&
|
||||||
|
SelectionUnit != DataGridSelectionUnit.Cell && SelectableItemCount > 0;
|
||||||
|
e.Handled = true;
|
||||||
|
}));
|
||||||
|
LoadingRow += (_, e) => UpdateRowNumber(e.Row);
|
||||||
|
ItemContainerGenerator.ItemsChanged += (_, _) => QueueHeaderUpdate();
|
||||||
|
Loaded += (_, _) => QueueHeaderUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void HeaderOptionsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||||
|
=> ((StyledDataGrid)d).QueueHeaderUpdate();
|
||||||
|
|
||||||
|
protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
|
||||||
|
{
|
||||||
|
base.OnItemsChanged(e);
|
||||||
|
QueueHeaderUpdate();
|
||||||
|
UpdateSelectAllState();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int SelectableItemCount => Items.Count - (Items.Contains(System.Windows.Data.CollectionView.NewItemPlaceholder) ? 1 : 0);
|
||||||
|
|
||||||
|
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
|
||||||
|
{
|
||||||
|
base.OnSelectionChanged(e);
|
||||||
|
UpdateSelectAllState();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateSelectAllState()
|
||||||
|
{
|
||||||
|
SetValue(AreAllRowsSelectedPropertyKey, SelectedItems.Count == 0 ? false :
|
||||||
|
SelectedItems.Count == SelectableItemCount ? true : (bool?)null);
|
||||||
|
CommandManager.InvalidateRequerySuggested();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
|
||||||
|
{
|
||||||
|
base.OnPropertyChanged(e);
|
||||||
|
if (e.Property == FontSizeProperty || e.Property == FontFamilyProperty ||
|
||||||
|
e.Property == FontStyleProperty || e.Property == FontWeightProperty || e.Property == FontStretchProperty)
|
||||||
|
QueueHeaderUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool headerUpdatePending;
|
||||||
|
private void QueueHeaderUpdate()
|
||||||
|
{
|
||||||
|
if (headerUpdatePending) return;
|
||||||
|
headerUpdatePending = true;
|
||||||
|
Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
|
||||||
|
{
|
||||||
|
headerUpdatePending = false;
|
||||||
|
// 使用最宽数字估算所有行号,避免虚拟化滚动时行头宽度跳变。
|
||||||
|
var digits = Math.Max(1, Items.Count.ToString(CultureInfo.InvariantCulture).Length);
|
||||||
|
var numberWidth = 0d;
|
||||||
|
if (ShowRowNumber)
|
||||||
|
for (var digit = '0'; digit <= '9'; digit++)
|
||||||
|
{
|
||||||
|
var text = new FormattedText(new string(digit, digits), CultureInfo.CurrentCulture,
|
||||||
|
FlowDirection, new Typeface(FontFamily, FontStyle, FontWeight, FontStretch),
|
||||||
|
FontSize, Foreground ?? Brushes.Black, VisualTreeHelper.GetDpi(this).PixelsPerDip);
|
||||||
|
numberWidth = Math.Max(numberWidth, Math.Ceiling(text.WidthIncludingTrailingWhitespace) + 16);
|
||||||
|
}
|
||||||
|
SetCurrentValue(RowHeaderWidthProperty, numberWidth + (ShowCheckBox ? 32 : 0) +
|
||||||
|
(ShowRowNumber && ShowCheckBox ? 1 : 0) + (ShowRowNumber || ShowCheckBox ? 1 : 0));
|
||||||
|
foreach (var row in GetRealizedRows(this)) UpdateRowNumber(row);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateRowNumber(DataGridRow row)
|
||||||
|
=> row.SetCurrentValue(DataGridRow.HeaderProperty, ShowRowNumber ? (row.GetIndex() + 1).ToString(CultureInfo.CurrentCulture) : null);
|
||||||
|
|
||||||
|
private IEnumerable<DataGridRow> GetRealizedRows(DependencyObject parent)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
|
||||||
|
{
|
||||||
|
var child = VisualTreeHelper.GetChild(parent, i);
|
||||||
|
if (child is DataGridRow row && ItemsControl.ItemsControlFromItemContainer(row) == this) yield return row;
|
||||||
|
else if (child is not DataGrid)
|
||||||
|
foreach (var descendant in GetRealizedRows(child)) yield return descendant;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,249 @@
|
|||||||
|
<ResourceDictionary
|
||||||
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:local="clr-namespace:Main.Test">
|
||||||
|
<ResourceDictionary.MergedDictionaries>
|
||||||
|
<ResourceDictionary Source="/Main.Test;component/Dictionary1.xaml" />
|
||||||
|
</ResourceDictionary.MergedDictionaries>
|
||||||
|
<Style x:Key="StyledDefaultCellStyle" TargetType="DataGridCell">
|
||||||
|
<Setter Property="BorderThickness" Value="0" />
|
||||||
|
<Setter Property="Background" Value="Transparent" />
|
||||||
|
<Setter Property="Padding" Value="8,0" />
|
||||||
|
<Setter Property="FontSize" Value="{DynamicResource DefaultFontSize}" />
|
||||||
|
<Setter Property="FontFamily" Value="{DynamicResource FontFamily}" />
|
||||||
|
<!--<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGrid, Mode=FindAncestor}, Path=Foreground, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />-->
|
||||||
|
<Setter Property="VerticalContentAlignment" Value="Center" />
|
||||||
|
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
|
||||||
|
<Setter Property="Template">
|
||||||
|
<Setter.Value>
|
||||||
|
<ControlTemplate TargetType="{x:Type DataGridCell}">
|
||||||
|
<Grid>
|
||||||
|
<Border
|
||||||
|
Padding="{TemplateBinding Padding}"
|
||||||
|
Background="{TemplateBinding Background}"
|
||||||
|
BorderBrush="{TemplateBinding BorderBrush}"
|
||||||
|
BorderThickness="{TemplateBinding BorderThickness}">
|
||||||
|
<ContentPresenter
|
||||||
|
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
|
||||||
|
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
|
||||||
|
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
|
||||||
|
</Border>
|
||||||
|
</Grid>
|
||||||
|
<ControlTemplate.Triggers>
|
||||||
|
<Trigger Property="IsSelected" Value="True">
|
||||||
|
<Setter Property="Background" Value="Transparent" />
|
||||||
|
<Setter Property="Foreground" Value="{Binding SelectedRowForeground, RelativeSource={RelativeSource AncestorType={x:Type local:StyledDataGrid}}}" />
|
||||||
|
</Trigger>
|
||||||
|
</ControlTemplate.Triggers>
|
||||||
|
</ControlTemplate>
|
||||||
|
</Setter.Value>
|
||||||
|
</Setter>
|
||||||
|
</Style>
|
||||||
|
<Style x:Key="StyledDefaultRowStyle" TargetType="{x:Type DataGridRow}">
|
||||||
|
<Setter Property="BorderThickness" Value="0,0,1,0" />
|
||||||
|
<Setter Property="Background" Value="{x:Null}" />
|
||||||
|
<Setter Property="FontSize" Value="{DynamicResource DefaultFontSize}" />
|
||||||
|
<Setter Property="FontFamily" Value="{DynamicResource FontFamily}" />
|
||||||
|
<Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGrid, Mode=FindAncestor}, Path=BorderBrush}" />
|
||||||
|
<Setter Property="Template">
|
||||||
|
<Setter.Value>
|
||||||
|
<ControlTemplate TargetType="DataGridRow">
|
||||||
|
<Grid>
|
||||||
|
<Border
|
||||||
|
x:Name="border"
|
||||||
|
Background="{TemplateBinding Background}"
|
||||||
|
BorderBrush="{TemplateBinding BorderBrush}"
|
||||||
|
BorderThickness="{TemplateBinding BorderThickness}" />
|
||||||
|
<SelectiveScrollingGrid>
|
||||||
|
<SelectiveScrollingGrid.ColumnDefinitions>
|
||||||
|
<ColumnDefinition Width="Auto" />
|
||||||
|
<ColumnDefinition Width="*" />
|
||||||
|
</SelectiveScrollingGrid.ColumnDefinitions>
|
||||||
|
<SelectiveScrollingGrid.RowDefinitions>
|
||||||
|
<RowDefinition Height="*" />
|
||||||
|
<RowDefinition Height="Auto" />
|
||||||
|
</SelectiveScrollingGrid.RowDefinitions>
|
||||||
|
<DataGridCellsPresenter
|
||||||
|
Grid.Column="1"
|
||||||
|
ItemsPanel="{TemplateBinding ItemsPanel}"
|
||||||
|
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
|
||||||
|
<DataGridDetailsPresenter
|
||||||
|
x:Name="DetailsPresenter"
|
||||||
|
Grid.Row="1"
|
||||||
|
Grid.Column="1"
|
||||||
|
SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
|
||||||
|
Visibility="{TemplateBinding DetailsVisibility}" />
|
||||||
|
<DataGridRowHeader
|
||||||
|
Grid.RowSpan="2"
|
||||||
|
SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"
|
||||||
|
Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
|
||||||
|
</SelectiveScrollingGrid>
|
||||||
|
<Grid
|
||||||
|
x:Name="fousIcon"
|
||||||
|
IsHitTestVisible="False"
|
||||||
|
Opacity="0"
|
||||||
|
Visibility="Collapsed">
|
||||||
|
<Path
|
||||||
|
Width="10"
|
||||||
|
Height="10"
|
||||||
|
HorizontalAlignment="Left"
|
||||||
|
VerticalAlignment="Top"
|
||||||
|
Data="M0,0 L0,0 50,0 50,10 10,10 10,50 0,50 0,52 z"
|
||||||
|
Fill="#4BB2FF"
|
||||||
|
RenderTransformOrigin="0.5,0.5"
|
||||||
|
Stretch="Fill">
|
||||||
|
<Path.RenderTransform>
|
||||||
|
<TransformGroup>
|
||||||
|
<ScaleTransform />
|
||||||
|
<SkewTransform />
|
||||||
|
<RotateTransform Angle="0" />
|
||||||
|
<TranslateTransform />
|
||||||
|
</TransformGroup>
|
||||||
|
</Path.RenderTransform>
|
||||||
|
</Path>
|
||||||
|
<Path
|
||||||
|
Width="10"
|
||||||
|
Height="10"
|
||||||
|
HorizontalAlignment="Right"
|
||||||
|
VerticalAlignment="Top"
|
||||||
|
Data="M0,0 L0,0 50,0 50,10 10,10 10,50 0,50 0,52 z"
|
||||||
|
Fill="#4BB2FF"
|
||||||
|
RenderTransformOrigin="0.5,0.5"
|
||||||
|
Stretch="Fill">
|
||||||
|
<Path.RenderTransform>
|
||||||
|
<TransformGroup>
|
||||||
|
<ScaleTransform />
|
||||||
|
<SkewTransform />
|
||||||
|
<RotateTransform Angle="90" />
|
||||||
|
<TranslateTransform />
|
||||||
|
</TransformGroup>
|
||||||
|
</Path.RenderTransform>
|
||||||
|
</Path>
|
||||||
|
<Path
|
||||||
|
Width="10"
|
||||||
|
Height="10"
|
||||||
|
HorizontalAlignment="Right"
|
||||||
|
VerticalAlignment="Bottom"
|
||||||
|
Data="M0,0 L0,0 50,0 50,10 10,10 10,50 0,50 0,52 z"
|
||||||
|
Fill="#4BB2FF"
|
||||||
|
RenderTransformOrigin="0.5,0.5"
|
||||||
|
Stretch="Fill">
|
||||||
|
<Path.RenderTransform>
|
||||||
|
<TransformGroup>
|
||||||
|
<ScaleTransform />
|
||||||
|
<SkewTransform />
|
||||||
|
<RotateTransform Angle="180" />
|
||||||
|
<TranslateTransform />
|
||||||
|
</TransformGroup>
|
||||||
|
</Path.RenderTransform>
|
||||||
|
</Path>
|
||||||
|
<Path
|
||||||
|
Width="10"
|
||||||
|
Height="10"
|
||||||
|
HorizontalAlignment="Left"
|
||||||
|
VerticalAlignment="Bottom"
|
||||||
|
Data="M0,0 L0,0 50,0 50,10 10,10 10,50 0,50 0,52 z"
|
||||||
|
Fill="#4BB2FF"
|
||||||
|
RenderTransformOrigin="0.5,0.5"
|
||||||
|
Stretch="Fill">
|
||||||
|
<Path.RenderTransform>
|
||||||
|
<TransformGroup>
|
||||||
|
<ScaleTransform />
|
||||||
|
<SkewTransform />
|
||||||
|
<RotateTransform Angle="-90" />
|
||||||
|
<TranslateTransform />
|
||||||
|
</TransformGroup>
|
||||||
|
</Path.RenderTransform>
|
||||||
|
</Path>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
<ControlTemplate.Triggers>
|
||||||
|
<Trigger Property="IsSelected" Value="True">
|
||||||
|
<Setter TargetName="fousIcon" Property="Opacity" Value="1" />
|
||||||
|
<Setter Property="Foreground" Value="{Binding SelectedRowForeground, RelativeSource={RelativeSource AncestorType={x:Type local:StyledDataGrid}}}" />
|
||||||
|
<Setter TargetName="border" Property="Background" Value="{Binding SelectedRowBackground, RelativeSource={RelativeSource AncestorType={x:Type local:StyledDataGrid}}}" />
|
||||||
|
</Trigger>
|
||||||
|
<Trigger Property="DetailsVisibility" Value="Visible">
|
||||||
|
<Setter TargetName="DetailsPresenter" Property="Margin" Value="0,0,1,0" />
|
||||||
|
</Trigger>
|
||||||
|
|
||||||
|
</ControlTemplate.Triggers>
|
||||||
|
</ControlTemplate>
|
||||||
|
</Setter.Value>
|
||||||
|
</Setter>
|
||||||
|
</Style>
|
||||||
|
<Style
|
||||||
|
x:Key="StyledRowHeaderStyle"
|
||||||
|
BasedOn="{StaticResource DefaultRowHeaderStyle}"
|
||||||
|
TargetType="DataGridRowHeader">
|
||||||
|
<Setter Property="Template">
|
||||||
|
<Setter.Value>
|
||||||
|
<ControlTemplate TargetType="DataGridRowHeader">
|
||||||
|
<Border
|
||||||
|
Background="Transparent"
|
||||||
|
BorderBrush="{TemplateBinding BorderBrush}"
|
||||||
|
BorderThickness="0,0,1,1">
|
||||||
|
<DockPanel LastChildFill="True">
|
||||||
|
<Border
|
||||||
|
x:Name="CheckArea"
|
||||||
|
Width="32"
|
||||||
|
DockPanel.Dock="Right"
|
||||||
|
Visibility="Collapsed">
|
||||||
|
<CheckBox
|
||||||
|
HorizontalAlignment="Center"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
AutomationProperties.Name="选择行"
|
||||||
|
Focusable="True"
|
||||||
|
IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
|
||||||
|
</Border>
|
||||||
|
<Border
|
||||||
|
x:Name="Divider"
|
||||||
|
Width="1"
|
||||||
|
Background="{TemplateBinding BorderBrush}"
|
||||||
|
DockPanel.Dock="Right"
|
||||||
|
Visibility="Collapsed" />
|
||||||
|
<ContentPresenter
|
||||||
|
x:Name="Number"
|
||||||
|
Content="{Binding Header, RelativeSource={RelativeSource AncestorType=DataGridRow}}"
|
||||||
|
Margin="8,0"
|
||||||
|
HorizontalAlignment="Center"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
Visibility="Collapsed" />
|
||||||
|
</DockPanel>
|
||||||
|
</Border>
|
||||||
|
<ControlTemplate.Triggers>
|
||||||
|
<DataTrigger Binding="{Binding ShowRowNumber, RelativeSource={RelativeSource AncestorType=local:StyledDataGrid}}" Value="True">
|
||||||
|
<Setter TargetName="Number" Property="Visibility" Value="Visible" />
|
||||||
|
</DataTrigger>
|
||||||
|
<DataTrigger Binding="{Binding ShowCheckBox, RelativeSource={RelativeSource AncestorType=local:StyledDataGrid}}" Value="True">
|
||||||
|
<Setter TargetName="CheckArea" Property="Visibility" Value="Visible" />
|
||||||
|
</DataTrigger>
|
||||||
|
<MultiDataTrigger>
|
||||||
|
<MultiDataTrigger.Conditions>
|
||||||
|
<Condition Binding="{Binding ShowRowNumber, RelativeSource={RelativeSource AncestorType=local:StyledDataGrid}}" Value="True" />
|
||||||
|
<Condition Binding="{Binding ShowCheckBox, RelativeSource={RelativeSource AncestorType=local:StyledDataGrid}}" Value="True" />
|
||||||
|
</MultiDataTrigger.Conditions>
|
||||||
|
<Setter TargetName="Divider" Property="Visibility" Value="Visible" />
|
||||||
|
</MultiDataTrigger>
|
||||||
|
</ControlTemplate.Triggers>
|
||||||
|
</ControlTemplate>
|
||||||
|
</Setter.Value>
|
||||||
|
</Setter>
|
||||||
|
</Style>
|
||||||
|
|
||||||
|
<Style
|
||||||
|
x:Key="StyledNumberCellStyle"
|
||||||
|
BasedOn="{StaticResource StyledDefaultCellStyle}"
|
||||||
|
TargetType="DataGridCell">
|
||||||
|
<Setter Property="HorizontalContentAlignment" Value="Right" />
|
||||||
|
</Style>
|
||||||
|
|
||||||
|
<Style BasedOn="{StaticResource DefaultDataGridStyle}" TargetType="local:StyledDataGrid">
|
||||||
|
|
||||||
|
<Setter Property="SelectionMode" Value="Extended" />
|
||||||
|
<Setter Property="SelectionUnit" Value="FullRow" />
|
||||||
|
<Setter Property="RowStyle" Value="{StaticResource StyledDefaultRowStyle}" />
|
||||||
|
<Setter Property="CellStyle" Value="{StaticResource StyledDefaultCellStyle}" />
|
||||||
|
<Setter Property="RowHeaderStyle" Value="{StaticResource StyledRowHeaderStyle}" />
|
||||||
|
</Style>
|
||||||
|
</ResourceDictionary>
|
||||||
@@ -32,7 +32,7 @@ internal class MainViewModel
|
|||||||
var singleItem = itemFaker.Generate();
|
var singleItem = itemFaker.Generate();
|
||||||
|
|
||||||
// 生成100个对象
|
// 生成100个对象
|
||||||
Data = itemFaker.Generate(100);
|
Data = itemFaker.Generate(1000);
|
||||||
//Data.Clear();
|
//Data.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user