Skip to content

Commit 3bce6e5

Browse files
authored
Merge pull request w-ahmad#374 from cricketthomas/293-select-after-context-requested
Add option to select row or cell on context request
2 parents ba91094 + 3435298 commit 3bce6e5

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
ItemsSource="{Binding Items}"
2626
CellContextFlyoutOpening="OnCellContextFlyoutOpening"
2727
RowContextFlyoutOpening="OnRowContextFlyoutOpening"
28+
ForceRowOrCellSelectionOnContextRequested="{x:Bind ForceRowOrCellSelectionToggleSwitch.IsOn, Mode=OneWay}"
2829
AutoGeneratingColumn="{x:Bind local:ExampleModelColumnsHelper.OnAutoGeneratingColumns}">
2930
<tv:TableView.RowContextFlyout>
3031
<MenuFlyout>
@@ -75,12 +76,26 @@
7576
</tv:TableView.CellContextFlyout>
7677
</tv:TableView>
7778
</controls:SamplePresenter.Example>
79+
<controls:SamplePresenter.Options>
80+
<StackPanel MaxWidth="250" Spacing="16">
81+
<ToggleSwitch
82+
x:Name="ForceRowOrCellSelectionToggleSwitch"
83+
Header="Force Row or Cell to be selected on ContextFlyoutOpening"
84+
IsOn="True"
85+
OffContent="False"
86+
OnContent="True" />
87+
</StackPanel>
88+
</controls:SamplePresenter.Options>
7889
<controls:SamplePresenter.Xaml>
7990
<x:String xml:space="preserve">
8091
&lt;tv:TableView x:Name="tableView"
81-
ItemsSource="{Binding Items}" /&gt;
92+
ItemsSource="{Binding Items}"
93+
ForceRowOrCellSelectionOnContextRequested="$(ForceRowOrCellSelect)" /&gt;
8294
</x:String>
8395
</controls:SamplePresenter.Xaml>
96+
<controls:SamplePresenter.Substitutions>
97+
<controls:CodeSubstitution Key="ForceRowOrCellSelect" Value="{x:Bind tableView.ForceRowOrCellSelectionOnContextRequested, Mode=OneWay}" />
98+
</controls:SamplePresenter.Substitutions>
8499
</controls:SamplePresenter>
85100
</Grid>
86101
</Page>

src/TableView.Properties.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ public partial class TableView
266266
/// </summary>
267267
public static readonly DependencyProperty ShowFilterItemsCountProperty = DependencyProperty.Register(nameof(ShowFilterItemsCount), typeof(bool), typeof(TableView), new PropertyMetadata(false));
268268

269+
270+
/// <summary>
271+
/// Identifies the <see cref="ForceRowOrCellSelectionOnContextRequested"/> dependency property.
272+
/// </summary>
273+
public static readonly DependencyProperty ForceRowOrCellSelectionOnContextRequestedProperty = DependencyProperty.Register(nameof(ForceRowOrCellSelectionOnContextRequested), typeof(bool), typeof(TableView), new PropertyMetadata(false));
274+
269275
/// <summary>
270276
/// Gets or sets a value indicating whether opening the column filter over header right-click is enabled.
271277
/// </summary>
@@ -331,6 +337,15 @@ public bool ShowFilterItemsCount
331337
set => SetValue(ShowFilterItemsCountProperty, value);
332338
}
333339

340+
/// <summary>
341+
/// Gets or sets a value that indicates whether the TableView should force select the Row or Cell depending on the SelectionUnit
342+
/// </summary>
343+
public bool ForceRowOrCellSelectionOnContextRequested
344+
{
345+
get => (bool)GetValue(ForceRowOrCellSelectionOnContextRequestedProperty);
346+
set => SetValue(ForceRowOrCellSelectionOnContextRequestedProperty, value);
347+
}
348+
334349
/// <summary>
335350
/// Gets or sets the selection start cell slot.
336351
/// </summary>

src/TableViewCell.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ private void OnContextRequested(UIElement sender, ContextRequestedEventArgs e)
6464
{
6565
if (!e.TryGetPosition(sender, out var position)) return;
6666
#endif
67+
68+
// Select the cell before showing the Context Menu
69+
if (TableView is not null && TableView.ForceRowOrCellSelectionOnContextRequested && !IsSelected)
70+
{
71+
TableView.MakeSelection(Slot, false);
72+
}
73+
6774
e.Handled = TableView?.ShowCellContext(this, position) is true;
6875
}
6976

src/TableViewRow.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ private void OnContextRequested(UIElement sender, ContextRequestedEventArgs e)
7070
{
7171
if (!e.TryGetPosition(sender, out var position)) return;
7272
#endif
73+
74+
// Select the row before showing the Context Menu
75+
if (TableView is not null && TableView.ForceRowOrCellSelectionOnContextRequested && !IsSelected)
76+
{
77+
TableView.MakeSelection(new TableViewCellSlot(Index, -1), false);
78+
}
79+
7380
e.Handled = TableView?.ShowRowContext(this, position) is true;
7481
}
7582

0 commit comments

Comments
 (0)