Skip to content

Commit 3f29bde

Browse files
author
msftbot[bot]
authored
fix: Prevent focus reset for DataGridTemplateColumn (#4206)
<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 --> <!-- 👉 It is imperative to resolve ONE ISSUE PER PR and avoid making multiple changes unless the changes interrelate with each other --> <!-- 📝 Please always keep the "☑️ Allow edits by maintainers" button checked in the Pull Request Template as it increases collaboration with the Toolkit maintainers by permitting commits to your PR branch (only) created from your fork. This can let us quickly make fixes for minor typos or forgotten StyleCop issues during review without needing to wait on you doing extra work. Let us help you help us! 🎉 --> ### Note: This is a copy of the previously reviewed work with a new branch name ## Fixes #2493 <!-- Add the relevant issue number after the "#" mentioned above (for ex: "## Fixes #1234") which will automatically close the issue once the PR is merged. --> <!-- Add a brief overview here of the feature/bug & fix. --> This prevents DataGrid from assuming focus should be reset back to itself when a control inside DataGridTemplateColumn gains focus. This is my first WCT PR, so please go easy on me 😆 ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more options below that apply to this PR. --> Bugfix <!-- - --> <!-- - Feature --> <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> Trying to open a control like CalendarDatePicker from within a DataGridTemplateColumn doesn't work, as there is specific behavior to detect when DataGrid loses focus and return it. This doesn't make much sense for controls which require focus to function properly. ## What is the new behavior? <!-- Describe how was this issue resolved or changed? --> When the DataGrid loses focus, we now specifically detect if the editing column is a DataGridTemplateColumn and alter the behavior to accommodate this. ## PR Checklist Please check if your PR fulfills the following requirements: - [x] Tested code with current [supported SDKs](../readme.md#supported) - [ ] Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link --> - [ ] Sample in sample app has been added / updated (for bug fixes / features) - [ ] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/CommunityToolkit/WindowsCommunityToolkit-design-assets) - [ ] New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/CommunityToolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc... - [ ] Tests for the changes have been added (for bug fixes / features) (if applicable) - [ ] Header has been added to all new source files (run *build/UpdateHeaders.bat*) - [x] Contains **NO** breaking changes <!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. Please note that breaking changes are likely to be rejected within minor release cycles or held until major versions. --> ## Other information I'm unsure if this is the *best* way to do this, but it makes the most sense given how DataGrid already has the capability to detect the editing element and column type. Additional context before branch rename: #4206 Related: unoplatform/uno#6543
2 parents 20f951e + 9cf1630 commit 3f29bde

File tree

8 files changed

+169
-133
lines changed

8 files changed

+169
-133
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Assets/mtns.csv

Lines changed: 117 additions & 117 deletions
Large diffs are not rendered by default.

Microsoft.Toolkit.Uwp.SampleApp/Data/DataGridDataItem.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ public string Parent_mountain
149149

150150
public uint Prominence { get; set; }
151151

152-
public uint First_ascent { get; set; }
152+
// You need to use DateTimeOffset to get proper binding to the CalendarDatePicker control, DateTime won't work.
153+
public DateTimeOffset First_ascent { get; set; }
153154

154155
public string Ascents { get; set; }
155156

Microsoft.Toolkit.Uwp.SampleApp/Data/DataGridDataSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public async Task<IEnumerable<DataGridDataItem>> GetDataAsync()
4848
Coordinates = values[4],
4949
Prominence = uint.Parse(values[5]),
5050
Parent_mountain = values[6],
51-
First_ascent = uint.Parse(values[7]),
52-
Ascents = values[8]
51+
First_ascent = DateTimeOffset.Parse(values[7]),
52+
Ascents = values[8],
5353
});
5454
}
5555
}

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DataGrid/DataGridCode.bind

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
4+
xmlns:converters="using:Microsoft.Toolkit.Uwp.UI.Converters"
45
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
56
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
67
mc:Ignorable="d">
78

89
<Grid>
910
<Grid.Resources>
11+
<converters:StringFormatConverter x:Key="StringFormatConverter"/>
1012
<DataTemplate x:Key="RowDetailsTemplate">
1113
<StackPanel>
1214
<TextBlock Margin="20" Text="Here are the details for the selected mountain:" />
@@ -85,7 +87,19 @@
8587
<controls:DataGridTextColumn Header="Height (m)" Binding="{Binding Height_m}" Tag="Height_m" />
8688
<controls:DataGridTextColumn Header="Range" Binding="{Binding Range}" Tag="Range" />
8789
<controls:DataGridTextColumn Header="Parent Mountain" Binding="{Binding Parent_mountain}" Tag="Parent_mountain" />
90+
<controls:DataGridTemplateColumn Header="First Ascent" Tag="First_ascent">
91+
<controls:DataGridTemplateColumn.CellTemplate>
92+
<DataTemplate>
93+
<TextBlock Text="{Binding First_ascent, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:MM/dd/yyy}'}" VerticalAlignment="Center" Margin="8,0,0,0"/>
94+
</DataTemplate>
95+
</controls:DataGridTemplateColumn.CellTemplate>
96+
<controls:DataGridTemplateColumn.CellEditingTemplate>
97+
<DataTemplate>
98+
<CalendarDatePicker Margin="3,4,3,3" Date="{Binding First_ascent, Mode=TwoWay}"/>
99+
</DataTemplate>
100+
</controls:DataGridTemplateColumn.CellEditingTemplate>
101+
</controls:DataGridTemplateColumn>
88102
</controls:DataGrid.Columns>
89103
</controls:DataGrid>
90104
</Grid>
91-
</Page>
105+
</Page>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DataGrid/DataGridPage.xaml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,5 @@
66
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
77
mc:Ignorable="d">
88

9-
<!-- Shallow Copy -->
10-
<Grid Visibility="Collapsed">
11-
<controls:DataGrid>
12-
<controls:DataGrid.Columns>
13-
<controls:DataGridTextColumn/>
14-
<controls:DataGridComboBoxColumn/>
15-
</controls:DataGrid.Columns>
16-
</controls:DataGrid>
17-
</Grid>
9+
<!-- Shallow Copy in XamlOnlyPage.xaml -->
1810
</Page>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DataGrid/DataGridPage.xaml.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ public async void OnXamlRendered(FrameworkElement control)
4141
dataGrid.Sorting += DataGrid_Sorting;
4242
dataGrid.LoadingRowGroup += DataGrid_LoadingRowGroup;
4343
dataGrid.ItemsSource = await viewModel.GetDataAsync();
44+
dataGrid.PreparingCellForEdit += DataGrid_PreparingCellForEdit;
4445

45-
var comboBoxColumn = dataGrid.Columns.FirstOrDefault(x => x.Tag.Equals("Mountain")) as DataGridComboBoxColumn;
46+
var comboBoxColumn = dataGrid.Columns.FirstOrDefault(x => x.Tag?.Equals("Mountain") == true) as DataGridComboBoxColumn;
4647
if (comboBoxColumn != null)
4748
{
4849
comboBoxColumn.ItemsSource = await viewModel.GetMountains();
@@ -111,6 +112,15 @@ public async void OnXamlRendered(FrameworkElement control)
111112
}
112113
}
113114

115+
private void DataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
116+
{
117+
if (e.Column is DataGridTemplateColumn column && (string)column?.Tag == "First_ascent" &&
118+
e.EditingElement is CalendarDatePicker calendar)
119+
{
120+
calendar.IsCalendarOpen = true;
121+
}
122+
}
123+
114124
private void DataGrid_LoadingRowGroup(object sender, DataGridRowGroupHeaderEventArgs e)
115125
{
116126
ICollectionViewGroup group = e.RowGroupHeader.CollectionViewGroup;

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/XamlOnlyPage.xaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454
<ui:AttachedDropShadow x:Key="AttachedDropShadow" />
5555
<controls:DropShadowPanel x:Key="DropShadowPanel"
5656
ui:Effects.Shadow="{StaticResource AttachedShadow}" />
57+
<controls:DataGrid x:Key="DataGrid">
58+
<controls:DataGrid.Columns>
59+
<controls:DataGridCheckBoxColumn />
60+
<controls:DataGridComboBoxColumn />
61+
<controls:DataGridTemplateColumn />
62+
<controls:DataGridTextColumn />
63+
</controls:DataGrid.Columns>
64+
</controls:DataGrid>
5765
</Page.Resources>
5866

5967
<Grid>
@@ -90,4 +98,4 @@
9098
</interactions:EventTriggerBehavior>
9199
</interactivity:Interaction.Behaviors>
92100
</Grid>
93-
</Page>
101+
</Page>

Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5729,6 +5729,7 @@ private void DataGrid_LostFocus(object sender, RoutedEventArgs e)
57295729
{
57305730
bool focusLeftDataGrid = true;
57315731
bool dataGridWillReceiveRoutedEvent = true;
5732+
DataGridColumn editingColumn = null;
57325733

57335734
// Walk up the visual tree of the newly focused element
57345735
// to determine if focus is still within DataGrid.
@@ -5768,7 +5769,17 @@ private void DataGrid_LostFocus(object sender, RoutedEventArgs e)
57685769
focusedDependencyObject = parent;
57695770
}
57705771

5771-
if (focusLeftDataGrid)
5772+
if (this.EditingRow != null && this.EditingColumnIndex != -1)
5773+
{
5774+
editingColumn = this.ColumnsItemsInternal[this.EditingColumnIndex];
5775+
5776+
if (focusLeftDataGrid && editingColumn is DataGridTemplateColumn)
5777+
{
5778+
dataGridWillReceiveRoutedEvent = false;
5779+
}
5780+
}
5781+
5782+
if (focusLeftDataGrid && !(editingColumn is DataGridTemplateColumn))
57725783
{
57735784
this.ContainsFocus = false;
57745785
if (this.EditingRow != null)

0 commit comments

Comments
 (0)