Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 5 additions & 34 deletions src/Columns/TableViewBoundColumn.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
using System;
using System.Linq.Expressions;
using System.Reflection;
using WinUI.TableView.Extensions;

namespace WinUI.TableView;

Expand All @@ -13,31 +9,6 @@ namespace WinUI.TableView;
public abstract class TableViewBoundColumn : TableViewColumn
{
private Binding _binding = new();
private Func<object, object?>? _funcCompiledPropertyPath;

/// <inheritdoc/>
public override object? GetCellContent(object? dataItem)
{
if (dataItem is null)
return null;

if (_funcCompiledPropertyPath is null && !string.IsNullOrWhiteSpace(PropertyPath))
_funcCompiledPropertyPath = dataItem.GetFuncCompiledPropertyPath(PropertyPath!);

if (_funcCompiledPropertyPath is not null)
dataItem = _funcCompiledPropertyPath(dataItem);

if (Binding?.Converter is not null)
{
dataItem = Binding.Converter.Convert(
dataItem,
typeof(object),
Binding.ConverterParameter,
Binding.ConverterLanguage);
}

return dataItem;
}

/// <summary>
/// Gets the property path for the binding.
Expand Down Expand Up @@ -70,13 +41,13 @@ public virtual Binding Binding
}

/// <summary>
/// Gets or sets the data binding used to retrieve cell content when copying to the clipboard.
/// If no explicit clipboard binding is set, the column's <see cref="Binding"/> is returned as a fallback.
/// Gets or sets the optional data binding used to perform operations on cell content, for example sorting, filtering and exporting.
/// If no explicit operation binding is set, the column's <see cref="Binding"/> is returned as a fallback.
/// </summary>
public override Binding? ClipboardContentBinding
public override Binding? OperationContentBinding
{
get => base.ClipboardContentBinding ?? Binding;
set => base.ClipboardContentBinding = value;
get => base.OperationContentBinding ?? Binding;
set => base.OperationContentBinding = value;
}

/// <summary>
Expand Down
47 changes: 41 additions & 6 deletions src/Columns/TableViewColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public abstract partial class TableViewColumn : DependencyObject
private bool _isFiltered;
private bool _isFrozen;
private Func<object, object?>? _funcCompiledPropertyPath;
private Func<object, object?>? _funcCompiledClipboardPropertyPath;
private Binding? _clipboardContentBinding;

/// <summary>
/// Initializes a new instance of the <see cref="TableViewColumn"/> class with default conditional cell styles.
Expand Down Expand Up @@ -107,7 +109,25 @@ internal void SetOwningTableView(TableView tableView)
/// <returns>The content of the cell.</returns>
public virtual object? GetCellContent(object? dataItem)
{
return default;
if (dataItem is null)
return null;

if (_funcCompiledPropertyPath is null && !string.IsNullOrWhiteSpace(OperationContentBindingPropertyPath))
_funcCompiledPropertyPath = dataItem.GetFuncCompiledPropertyPath(OperationContentBindingPropertyPath!);

if (_funcCompiledPropertyPath is not null)
dataItem = _funcCompiledPropertyPath(dataItem);

if (OperationContentBinding?.Converter is not null)
{
dataItem = OperationContentBinding.Converter.Convert(
dataItem,
typeof(object),
OperationContentBinding.ConverterParameter,
OperationContentBinding.ConverterLanguage);
}

return dataItem;
}

/// <summary>
Expand All @@ -120,11 +140,11 @@ internal void SetOwningTableView(TableView tableView)
if (dataItem is null)
return null;

if (_funcCompiledPropertyPath is null && !string.IsNullOrWhiteSpace(ClipboardContentBindingPropertyPath))
_funcCompiledPropertyPath = dataItem.GetFuncCompiledPropertyPath(ClipboardContentBindingPropertyPath!);
if (_funcCompiledClipboardPropertyPath is null && !string.IsNullOrWhiteSpace(ClipboardContentBindingPropertyPath))
_funcCompiledClipboardPropertyPath = dataItem.GetFuncCompiledPropertyPath(ClipboardContentBindingPropertyPath!);

if (_funcCompiledPropertyPath is not null)
dataItem = _funcCompiledPropertyPath(dataItem);
if (_funcCompiledClipboardPropertyPath is not null)
dataItem = _funcCompiledClipboardPropertyPath(dataItem);

if (ClipboardContentBinding?.Converter is not null)
{
Expand Down Expand Up @@ -365,10 +385,25 @@ internal set
}
}

/// <summary>
/// Gets or sets the optional data binding used to perform operations on cell content, for example sorting, filtering and exporting.
/// </summary>
public virtual Binding? OperationContentBinding { get; set; }

/// <summary>
/// Gets the property path for the <see cref="OperationContentBinding"/>.
/// </summary>
internal string? OperationContentBindingPropertyPath => OperationContentBinding?.Path?.Path;

/// <summary>
/// Gets or sets the data binding used to retrieve cell content when copying to the clipboard.
/// If no explicit clipboard binding is set, the column's <see cref="OperationContentBinding"/> is returned as a fallback.
/// </summary>
public virtual Binding? ClipboardContentBinding { get; set; }
public Binding? ClipboardContentBinding
{
get => _clipboardContentBinding ?? OperationContentBinding;
set => _clipboardContentBinding = value;
}

/// <summary>
/// Gets the property path for the <see cref="ClipboardContentBinding"/>.
Expand Down
2 changes: 2 additions & 0 deletions src/Columns/TableViewTemplateColumn.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Markup;

namespace WinUI.TableView;

Expand All @@ -9,6 +10,7 @@ namespace WinUI.TableView;
#if WINDOWS
[WinRT.GeneratedBindableCustomProperty]
#endif
[ContentProperty(Name = nameof(CellTemplate))]
public partial class TableViewTemplateColumn : TableViewColumn
{
/// <summary>
Expand Down
54 changes: 45 additions & 9 deletions src/TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
}

/// <inheritdoc/>
protected override async void OnKeyDown(KeyRoutedEventArgs e)
protected override void OnKeyDown(KeyRoutedEventArgs e)
{
var shiftKey = KeyboardHelper.IsShiftKeyDown();
var ctrlKey = KeyboardHelper.IsCtrlKeyDown();
Expand All @@ -144,19 +144,19 @@
return;
}

await HandleNavigations(e, shiftKey, ctrlKey);
HandleNavigations(e, shiftKey, ctrlKey);
}

/// <summary>
/// Handles navigation keys.
/// </summary>
private async Task HandleNavigations(KeyRoutedEventArgs e, bool shiftKey, bool ctrlKey)
private void HandleNavigations(KeyRoutedEventArgs e, bool shiftKey, bool ctrlKey)
{
var currentCell = CurrentCellSlot.HasValue ? GetCellFromSlot(CurrentCellSlot.Value) : default;

if (e.Key is VirtualKey.F2 && currentCell is { IsReadOnly: false } && !IsEditing)
{
e.Handled = await currentCell.BeginCellEditing(e);
e.Handled = currentCell.BeginCellEditing(e);
}
else if (e.Key is VirtualKey.Escape && currentCell is not null && IsEditing)
{
Expand Down Expand Up @@ -192,7 +192,7 @@
{
if (!EndCellEditing(TableViewEditAction.Commit, currentCell)) return;

if (CurrentCellSlot == newSlot || GetCellFromSlot(newSlot) is not { } nextCell || !await nextCell.BeginCellEditing(e))
if (CurrentCellSlot == newSlot || GetCellFromSlot(newSlot) is not { } nextCell || !nextCell.BeginCellEditing(e))
{
SetIsEditing(false);
}
Expand Down Expand Up @@ -230,6 +230,42 @@
MakeSelection(newSlot, shiftKey);
e.Handled = true;
}
else if (e.Key is VirtualKey.Home or VirtualKey.End)
{
var row = ctrlKey ? (e.Key == VirtualKey.Home ? 0 : _collectionView.Count - 1) : CurrentCellSlot?.Row;
var column = e.Key == VirtualKey.Home ? 0 : Columns.VisibleColumns.Count - 1;

var newSlot = new TableViewCellSlot(row ?? -1, column);
MakeSelection(newSlot, shiftKey);
e.Handled = true;
}
else if (e.Key is VirtualKey.PageDown or VirtualKey.PageUp)
{
var pageSize = CalculateAvailablePageSize();

var row = (LastSelectionUnit is TableViewSelectionUnit.Row ? CurrentRowIndex : CurrentCellSlot?.Row) ?? -1;
var column = CurrentCellSlot?.Column ?? -1;

var numRows = CollectionView.Count;
var nextRow = e.Key == VirtualKey.PageDown
? Math.Min(numRows - 1, row + pageSize)
: Math.Max(0, row - pageSize);

var newSlot = new TableViewCellSlot(nextRow, column);
MakeSelection(newSlot, shiftKey);
e.Handled = true;
}
}

/// <summary>
/// Calculates how many rows should be able to fit within the actual height of the table without scrolling.
/// </summary>
private int CalculateAvailablePageSize()
{
var rowHeight = RowHeight is not double.NaN ? RowHeight : RowMinHeight;
var headerHeight = HeaderRowHeight is not double.NaN ? HeaderRowHeight : HeaderRowMinHeight;
var availableHeight = ActualHeight - headerHeight;
return (int)Math.Floor(availableHeight / rowHeight);
}

internal bool EndCellEditing(TableViewEditAction editAction, TableViewCell cell)
Expand Down Expand Up @@ -283,7 +319,7 @@
_scrollViewer = GetTemplateChild("ScrollViewer") as ScrollViewer;
_headerRowDefinition = GetTemplateChild("HeaderRowDefinition") as RowDefinition;
if (_scrollViewer is not null) _scrollViewer.Loaded += OnScrollViewerLoaded;

if (IsLoaded)
{
while (ItemsPanelRoot is null) await Task.Yield();
Expand Down Expand Up @@ -320,12 +356,12 @@
Source = this
});
}

/// <summary>
/// Handles the Loaded event of the TableView control.
/// </summary>
private void OnLoaded(object sender, RoutedEventArgs e)
{
{
EnsureAutoColumns();
}

Expand Down Expand Up @@ -536,7 +572,7 @@

for (var col = minColumn; col <= maxColumn; col++)
{
if (Columns.VisibleColumns[col] is not TableViewBoundColumn column ||
if (Columns.VisibleColumns[col] is not TableViewColumn column ||
!slots.Contains(new TableViewCellSlot(row, col)))
{
stringBuilder.Append(separator);
Expand Down Expand Up @@ -597,7 +633,7 @@
}
else
{
foreach (var propertyInfo in dataType.GetProperties())

Check warning on line 636 in src/TableView.cs

View workflow job for this annotation

GitHub Actions / build

'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'WinUI.TableView.Extensions.ObjectExtensions.GetItemType(IEnumerable)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Check warning on line 636 in src/TableView.cs

View workflow job for this annotation

GitHub Actions / build

'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'WinUI.TableView.Extensions.ObjectExtensions.GetItemType(IEnumerable)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Check warning on line 636 in src/TableView.cs

View workflow job for this annotation

GitHub Actions / build (ARM64)

'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'WinUI.TableView.Extensions.ObjectExtensions.GetItemType(IEnumerable)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
{
var displayAttribute = propertyInfo.GetCustomAttributes().OfType<DisplayAttribute>().FirstOrDefault();
var autoGenerateField = displayAttribute?.GetAutoGenerateField();
Expand Down
46 changes: 34 additions & 12 deletions src/TableViewCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,14 @@ protected override void OnPointerExited(PointerRoutedEventArgs e)
}

/// <inheritdoc/>
protected override async void OnTapped(TappedRoutedEventArgs e)
protected override void OnTapped(TappedRoutedEventArgs e)
{
base.OnTapped(e);

if ((TableView?.IsEditing ?? false) &&
TableView.CurrentCellSlot != Slot &&
TableView.CurrentCellSlot.HasValue &&
TableView.GetCellFromSlot(TableView.CurrentCellSlot.Value) is { } currentCell)
if (!TryEndCurrentCellEdit())
{
e.Handled = !TableView.EndCellEditing(TableViewEditAction.Commit, currentCell);

if (e.Handled) return;
e.Handled = true;
return;
}

if (TableView?.CurrentCellSlot != Slot || TableView?.LastSelectionUnit is TableViewSelectionUnit.Row)
Expand All @@ -236,9 +232,15 @@ protected override void OnPointerPressed(PointerRoutedEventArgs e)
{
base.OnPointerPressed(e);

if (!TryEndCurrentCellEdit())
{
e.Handled = true;
return;
}

if (!KeyboardHelper.IsShiftKeyDown() && TableView is not null)
{
TableView.SelectionStartCellSlot = TableView.SelectionUnit is not TableViewSelectionUnit.Row || !IsReadOnly ? Slot : default; ;
TableView.SelectionStartCellSlot = TableView.SelectionUnit is not TableViewSelectionUnit.Row || !IsReadOnly ? Slot : default;
TableView.SelectionStartRowIndex = Index;
CapturePointer(e.Pointer);
}
Expand Down Expand Up @@ -278,6 +280,26 @@ protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e)
}
}

/// <summary>
/// Tries to end the current edit operation, if any.
/// </summary>
/// <returns>True if an edit operation was successfully ended, or there is no edit operation.
/// False if the current edit operation can not be ended.</returns>
private bool TryEndCurrentCellEdit()
{
if ((TableView?.IsEditing ?? false) &&
TableView.CurrentCellSlot != Slot &&
TableView.CurrentCellSlot.HasValue &&
TableView.GetCellFromSlot(TableView.CurrentCellSlot.Value) is { } currentCell)
{
if (!TableView.EndCellEditing(TableViewEditAction.Commit, currentCell)) return false;

TableView.SetIsEditing(false);
}

return true;
}

/// <summary>
/// Gets the height of the horizontal gridlines/>.
/// </summary>
Expand Down Expand Up @@ -309,7 +331,7 @@ private double GetHorizontalGridlineHeight()
}

/// <inheritdoc/>
protected override async void OnDoubleTapped(DoubleTappedRoutedEventArgs e)
protected override void OnDoubleTapped(DoubleTappedRoutedEventArgs e)
{
var eventArgs = new TableViewCellDoubleTappedEventArgs(Slot, this, Row?.Content);
TableView?.OnCellDoubleTapped(eventArgs);
Expand All @@ -321,7 +343,7 @@ protected override async void OnDoubleTapped(DoubleTappedRoutedEventArgs e)

if (!IsReadOnly && TableView is not null && !TableView.IsEditing && !Column?.UseSingleElement is true)
{
e.Handled = await BeginCellEditing(e);
e.Handled = BeginCellEditing(e);
}
else
{
Expand Down Expand Up @@ -370,7 +392,7 @@ private void MakeSelection()
/// <param name="editingArgs">The event data associated with the editing request. Cannot be null.</param>
/// <returns>A task that represents the asynchronous operation. The task result is <see langword="true"/> if cell editing was
/// successfully started; otherwise, <see langword="false"/> if the operation was canceled.</returns>
internal async Task<bool> BeginCellEditing(RoutedEventArgs editingArgs)
internal bool BeginCellEditing(RoutedEventArgs editingArgs)
{
var args = new TableViewBeginningEditEventArgs(this, Row?.Content, Column!, editingArgs);
TableView?.OnBeginningEdit(args);
Expand Down
Loading