作成日: 2024/02/13 最終更新日: 2024/02/13
文書種別
使用方法
詳細
◆.NET Framework
.NET FrameworkのFlexGridでは、UI操作による複数列のソート機能は備えられておりません。
PreviewMouseLeftButtonUpイベントを下記のように実装することにより、Ctrlキーを押しながら列ヘッダをクリックする操作で複数列のソートを実行できます。
◎サンプルコード(VB)
Imports System.ComponentModel
Imports C1.WPF.FlexGrid
Class MainWindow
Sub New()
InitializeComponent()
Dim rnd = New Random()
flexGrid.ItemsSource = Enumerable.Range(1, 100).Select(Function(x) New User With {.Id = rnd.Next(1, 100), .Name = $"Name {rnd.Next(1, 100)}", .Age = rnd.Next(18, 45)}).ToList()
C1FlexGridMultiColumnSortBehavior.Apply(flexGrid)
End Sub
End Class
Public Class C1FlexGridMultiColumnSortBehavior
Shared _applied As Boolean = False
Shared _flexGrid As C1FlexGrid
Public Shared Sub Apply(flexGrid As C1FlexGrid)
If _applied Then
Return
End If
_flexGrid = flexGrid
AddHandler _flexGrid.PreviewMouseLeftButtonUp, AddressOf OnFlexGridPreviewMouseLeftButtonUp
_applied = True
End Sub
Public Shared Sub Remove(flexGrid As C1FlexGrid)
If _applied = True And flexGrid.Equals(_flexGrid) Then
RemoveHandler flexGrid.PreviewMouseLeftButtonUp, AddressOf OnFlexGridPreviewMouseLeftButtonUp
_applied = False
End If
End Sub
Private Shared Sub OnFlexGridPreviewMouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs)
Dim hitTest = _flexGrid.HitTest(e)
If Keyboard.Modifiers = ModifierKeys.Control Then
If hitTest.CellType = CellType.ColumnHeader Then
Dim column = _flexGrid.Columns(hitTest.Column)
Dim sortDescriptions = _flexGrid.CollectionView.SortDescriptions
If _flexGrid.ActiveEditor IsNot Nothing Then
CType(_flexGrid.CollectionView, ListCollectionView).CommitEdit()
End If
If sortDescriptions.Any(Function(x) x.PropertyName = column.BoundPropertyName) Then
Dim sortDescription = sortDescriptions.FirstOrDefault(Function(x) x.PropertyName = column.BoundPropertyName)
Console.WriteLine(sortDescription.Direction)
If sortDescription.Direction = ListSortDirection.Ascending Then
sortDescriptions.Remove(sortDescription)
sortDescriptions.Add(New SortDescription(column.BoundPropertyName, ListSortDirection.Descending))
Else
sortDescriptions.Remove(sortDescription)
sortDescriptions.Add(New SortDescription(column.BoundPropertyName, ListSortDirection.Ascending))
End If
Else
sortDescriptions.Add(New SortDescription(column.BoundPropertyName, ListSortDirection.Ascending))
End If
e.Handled = True
End If
End If
End Sub
End Class
Public Class User
Public Property Id As Integer
Public Property Name As String
Public Property Age As Integer
End Class
◎サンプルコード(C#)
using System;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.ComponentModel;
using C1.WPF.FlexGrid;
namespace WpfCsFlexGridMultiColumnSort
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var rnd = new Random();
this.flexGrid.ItemsSource = Enumerable.Range(1, 100).Select(x => new User() { Id = rnd.Next(1, 100), Name = $"Name {rnd.Next(1, 100)}", Age = rnd.Next(18, 45) }).ToList();
C1FlexGridMultiColumnSortBehavior.Apply(this.flexGrid);
}
}
}
public class C1FlexGridMultiColumnSortBehavior
{
private static bool _applied = false;
private static C1FlexGrid _flexGrid;
public static void Apply(C1FlexGrid flexGrid)
{
if (_applied)
{
return;
}
_flexGrid = flexGrid;
_flexGrid.PreviewMouseLeftButtonUp += OnFlexGridPreviewMouseLeftButtonUp;
_applied = true;
}
public static void Remove(C1FlexGrid flexGrid)
{
if (_applied == true & flexGrid.Equals(_flexGrid))
{
flexGrid.PreviewMouseLeftButtonUp -= OnFlexGridPreviewMouseLeftButtonUp;
_applied = false;
}
}
private static void OnFlexGridPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var hitTest = _flexGrid.HitTest(e);
if (Keyboard.Modifiers == ModifierKeys.Control)
{
if (hitTest.CellType == CellType.ColumnHeader)
{
var column = _flexGrid.Columns[hitTest.Column];
var sortDescriptions = _flexGrid.CollectionView.SortDescriptions;
if (_flexGrid.ActiveEditor != null)
{
((ListCollectionView)_flexGrid.CollectionView).CommitEdit();
}
if (sortDescriptions.Any(x => (x.PropertyName ?? "") == (column.BoundPropertyName ?? "")))
{
var sortDescription = sortDescriptions.FirstOrDefault(x => (x.PropertyName ?? "") == (column.BoundPropertyName ?? ""));
Console.WriteLine((int)sortDescription.Direction);
if (sortDescription.Direction == ListSortDirection.Ascending)
{
sortDescriptions.Remove(sortDescription);
sortDescriptions.Add(new SortDescription(column.BoundPropertyName, ListSortDirection.Descending));
}
else
{
sortDescriptions.Remove(sortDescription);
sortDescriptions.Add(new SortDescription(column.BoundPropertyName, ListSortDirection.Ascending));
}
}
else
{
sortDescriptions.Add(new SortDescription(column.BoundPropertyName, ListSortDirection.Ascending));
}
e.Handled = true;
}
}
}
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
◆.NET 6+
.NETのFlexGridは、UI操作による複数列のソート機能が標準で備えられています。
ShiftキーまたはCtrlキーを押しながら列ヘッダをクリックすると、複数列のソートを実行できます。