52 lines
4.3 KiB
C#
52 lines
4.3 KiB
C#
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
|
|
namespace Main.Test;
|
|
|
|
public partial class StyledDataGrid
|
|
{
|
|
public static readonly DependencyProperty NoDataTipsProperty = DataGridHelper.NoDataTipsProperty.AddOwner(typeof(StyledDataGrid));
|
|
public string NoDataTips { get => (string)GetValue(NoDataTipsProperty); set => SetValue(NoDataTipsProperty, value); }
|
|
public static readonly DependencyProperty IsActiveProperty = DataGridHelper.IsActiveProperty.AddOwner(typeof(StyledDataGrid));
|
|
/// <summary>为 true 时抑制空数据提示;可在业务加载数据期间设置。</summary>
|
|
public bool IsActive { get => (bool)GetValue(IsActiveProperty); set => SetValue(IsActiveProperty, value); }
|
|
|
|
public static readonly DependencyProperty RowContextMenuProperty = DependencyProperty.Register(nameof(RowContextMenu), typeof(ContextMenu), typeof(StyledDataGrid), new PropertyMetadata(null));
|
|
public ContextMenu? RowContextMenu { get => (ContextMenu?)GetValue(RowContextMenuProperty); set => SetValue(RowContextMenuProperty, value); }
|
|
|
|
public static readonly DependencyProperty ShowSelectAllProperty = DependencyProperty.Register(nameof(ShowSelectAll), typeof(bool), typeof(StyledDataGrid), new PropertyMetadata(true));
|
|
public static readonly DependencyProperty ToggleSelectionOnRowHeaderClickProperty = DependencyProperty.Register(nameof(ToggleSelectionOnRowHeaderClick), typeof(bool), typeof(StyledDataGrid), new PropertyMetadata(true));
|
|
public static readonly DependencyProperty SelectRowOnRightClickProperty = DependencyProperty.Register(nameof(SelectRowOnRightClick), typeof(bool), typeof(StyledDataGrid), new PropertyMetadata(false));
|
|
public static readonly DependencyProperty PauseImageLoadingWhileDraggingProperty = DependencyProperty.Register(nameof(PauseImageLoadingWhileDragging), typeof(bool), typeof(StyledDataGrid), new PropertyMetadata(true, ImageOptionsChanged));
|
|
public static readonly DependencyProperty EnableImageCacheProperty = DependencyProperty.Register(nameof(EnableImageCache), typeof(bool), typeof(StyledDataGrid), new PropertyMetadata(true, ImageOptionsChanged));
|
|
private static readonly DependencyPropertyKey IsImageLoadingSuspendedPropertyKey = DependencyProperty.RegisterReadOnly(nameof(IsImageLoadingSuspended), typeof(bool), typeof(StyledDataGrid), new PropertyMetadata(false));
|
|
public static readonly DependencyProperty IsImageLoadingSuspendedProperty = IsImageLoadingSuspendedPropertyKey.DependencyProperty;
|
|
|
|
public bool ShowSelectAll { get => (bool)GetValue(ShowSelectAllProperty); set => SetValue(ShowSelectAllProperty, value); }
|
|
public bool ToggleSelectionOnRowHeaderClick { get => (bool)GetValue(ToggleSelectionOnRowHeaderClickProperty); set => SetValue(ToggleSelectionOnRowHeaderClickProperty, value); }
|
|
public bool SelectRowOnRightClick { get => (bool)GetValue(SelectRowOnRightClickProperty); set => SetValue(SelectRowOnRightClickProperty, value); }
|
|
/// <summary>默认开启;拖动时暂停并清空图片,停止滚动 500ms 后恢复。</summary>
|
|
public bool PauseImageLoadingWhileDragging { get => (bool)GetValue(PauseImageLoadingWhileDraggingProperty); set => SetValue(PauseImageLoadingWhileDraggingProperty, value); }
|
|
/// <summary>默认开启图片缓存;设为 false 时不读取或写入缓存。</summary>
|
|
public bool EnableImageCache { get => (bool)GetValue(EnableImageCacheProperty); set => SetValue(EnableImageCacheProperty, value); }
|
|
public bool IsImageLoadingSuspended => (bool)GetValue(IsImageLoadingSuspendedProperty);
|
|
|
|
private static void ImageOptionsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
=> ((StyledDataGrid)d).ApplyImageLoadingOptions();
|
|
|
|
protected override void OnPreviewMouseRightButtonDown(MouseButtonEventArgs e)
|
|
{
|
|
if (SelectRowOnRightClick && !e.Handled && SelectionUnit != DataGridSelectionUnit.Cell && e.OriginalSource is DependencyObject source)
|
|
{
|
|
var row = FindRow(source);
|
|
if (row != null && ItemsControl.ItemsControlFromItemContainer(row) == this && !row.IsSelected &&
|
|
row.Item != System.Windows.Data.CollectionView.NewItemPlaceholder)
|
|
{
|
|
UnselectAll(); row.SetCurrentValue(DataGridRow.IsSelectedProperty, true);
|
|
}
|
|
}
|
|
base.OnPreviewMouseRightButtonDown(e);
|
|
}
|
|
}
|