Skip to content

Commit 1612a18

Browse files
authored
Merge pull request w-ahmad#344 from godlytalias/feature/drag_selection_rectangle
Feature/drag selection rectangle
2 parents e264b4d + 1ce7123 commit 1612a18

7 files changed

Lines changed: 674 additions & 7 deletions

File tree

samples/WinUI.TableView.SampleApp/Pages/SelectionPage.xaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
OnContent="True"
3737
OffContent="False"
3838
IsOn="{Binding IsReadOnly, Mode=TwoWay, ElementName=tableView}" />
39+
<ToggleSwitch Header="ShowDragRectangle"
40+
OnContent="True"
41+
OffContent="False"
42+
IsOn="{Binding ShowDragRectangle, Mode=TwoWay, ElementName=tableView}" />
3943
<InfoBar IsOpen="True"
4044
IsClosable="False"
4145
Title="Selected Item">
@@ -79,7 +83,8 @@
7983
ItemsSource="{Binding Items}"
8084
SelectionMode="$(SelectionMode)"
8185
SelectionUnit="$(SelectionUnit)"
82-
IsReadOnly="$(IsReadOnly)" /&gt;
86+
IsReadOnly="$(IsReadOnly)"
87+
ShowDragRectangle="$(ShowDragRectangle)" /&gt;
8388
</x:String>
8489
</controls:SamplePresenter.Xaml>
8590
<controls:SamplePresenter.Substitutions>
@@ -89,6 +94,8 @@
8994
Value="{x:Bind tableView.SelectionUnit, Mode=OneWay}" />
9095
<controls:CodeSubstitution Key="IsReadOnly"
9196
Value="{x:Bind tableView.IsReadOnly, Mode=OneWay}" />
97+
<controls:CodeSubstitution Key="ShowDragRectangle"
98+
Value="{x:Bind tableView.ShowDragRectangle, Mode=OneWay}" />
9299
</controls:SamplePresenter.Substitutions>
93100
</controls:SamplePresenter>
94101
</Grid>

src/TableView.Properties.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ public bool CanPaste
300300
set => SetValue(CanPasteProperty, value);
301301
}
302302

303+
/// <summary>
304+
/// Identifies the <see cref="ShowDragRectangle"/> dependency property.
305+
/// </summary>
306+
public static readonly DependencyProperty ShowDragRectangleProperty = DependencyProperty.Register(nameof(ShowDragRectangle), typeof(bool), typeof(TableView), new PropertyMetadata(true, OnShowDragRectangleChanged));
307+
303308
/// <summary>
304309
/// Gets or sets a value indicating whether opening the column filter over header right-click is enabled.
305310
/// </summary>
@@ -365,6 +370,15 @@ public bool ShowFilterItemsCount
365370
set => SetValue(ShowFilterItemsCountProperty, value);
366371
}
367372

373+
/// <summary>
374+
/// Gets or sets a value indicating whether the drag selection rectangle is shown during cell drag selection.
375+
/// </summary>
376+
public bool ShowDragRectangle
377+
{
378+
get => (bool)GetValue(ShowDragRectangleProperty);
379+
set => SetValue(ShowDragRectangleProperty, value);
380+
}
381+
368382
/// <summary>
369383
/// Gets or sets a value that indicates whether the TableView should force select the Row or Cell depending on the SelectionUnit
370384
/// </summary>
@@ -404,6 +418,16 @@ public bool ForceRowOrCellSelectionOnContextRequested
404418
/// </summary>
405419
internal bool IsEditing { get; private set; }
406420

421+
/// <summary>
422+
/// Gets the canvas that hosts the drag selection rectangle.
423+
/// </summary>
424+
internal Canvas? DragRectangleCanvas { get; private set; }
425+
426+
/// <summary>
427+
/// Gets a value indicating whether a drag selection is currently in progress.
428+
/// </summary>
429+
internal bool IsDragSelecting { get; private set; }
430+
407431
/// <summary>
408432
/// Gets the visibility states of details pane for each item.
409433
/// </summary>
@@ -928,6 +952,23 @@ private static void OnIsReadOnlyChanged(DependencyObject d, DependencyPropertyCh
928952
}
929953
}
930954

955+
/// <summary>
956+
/// Handles changes to the ShowDragRectangle property.
957+
/// </summary>
958+
private static void OnShowDragRectangleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
959+
{
960+
if (d is TableView tableView && e.NewValue is false)
961+
{
962+
// Hide the rectangle visual but don't stop drag selection or auto-scroll
963+
if (tableView._dragRectangle is not null)
964+
{
965+
tableView._dragRectangle.Visibility = Visibility.Collapsed;
966+
}
967+
968+
tableView._dragStartPoint = null;
969+
}
970+
}
971+
931972
/// <summary>
932973
/// Handles changes to the CanFilterColumns property.
933974
/// </summary>

0 commit comments

Comments
 (0)