Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent focus reset for DataGridTemplateColumn #4206

Merged
4 commits merged into from
Sep 3, 2021
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
234 changes: 117 additions & 117 deletions Microsoft.Toolkit.Uwp.SampleApp/Assets/mtns.csv

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Microsoft.Toolkit.Uwp.SampleApp/Data/DataGridDataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ public string Parent_mountain

public uint Prominence { get; set; }

public uint First_ascent { get; set; }
// You need to use DateTimeOffset to get proper binding to the CalendarDatePicker control, DateTime won't work.
public DateTimeOffset First_ascent { get; set; }
michael-hawker marked this conversation as resolved.
Show resolved Hide resolved

public string Ascents { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions Microsoft.Toolkit.Uwp.SampleApp/Data/DataGridDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public async Task<IEnumerable<DataGridDataItem>> GetDataAsync()
Coordinates = values[4],
Prominence = uint.Parse(values[5]),
Parent_mountain = values[6],
First_ascent = uint.Parse(values[7]),
Ascents = values[8]
First_ascent = DateTimeOffset.Parse(values[7]),
Ascents = values[8],
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:converters="using:Microsoft.Toolkit.Uwp.UI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid>
<Grid.Resources>
<converters:StringFormatConverter x:Key="StringFormatConverter"/>
<DataTemplate x:Key="RowDetailsTemplate">
<StackPanel>
<TextBlock Margin="20" Text="Here are the details for the selected mountain:" />
Expand Down Expand Up @@ -85,7 +87,19 @@
<controls:DataGridTextColumn Header="Height (m)" Binding="{Binding Height_m}" Tag="Height_m" />
<controls:DataGridTextColumn Header="Range" Binding="{Binding Range}" Tag="Range" />
<controls:DataGridTextColumn Header="Parent Mountain" Binding="{Binding Parent_mountain}" Tag="Parent_mountain" />
<controls:DataGridTemplateColumn Header="First Ascent" Tag="First_ascent">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding First_ascent, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:MM/dd/yyy}'}" VerticalAlignment="Center" Margin="8,0,0,0"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
<controls:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<CalendarDatePicker Margin="3,4,3,3" Date="{Binding First_ascent, Mode=TwoWay}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellEditingTemplate>
</controls:DataGridTemplateColumn>
</controls:DataGrid.Columns>
</controls:DataGrid>
</Grid>
</Page>
</Page>
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,5 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<!-- Shallow Copy -->
<Grid Visibility="Collapsed">
<controls:DataGrid>
<controls:DataGrid.Columns>
<controls:DataGridTextColumn/>
<controls:DataGridComboBoxColumn/>
</controls:DataGrid.Columns>
</controls:DataGrid>
</Grid>
<!-- Shallow Copy in XamlOnlyPage.xaml -->
</Page>
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ public async void OnXamlRendered(FrameworkElement control)
dataGrid.Sorting += DataGrid_Sorting;
dataGrid.LoadingRowGroup += DataGrid_LoadingRowGroup;
dataGrid.ItemsSource = await viewModel.GetDataAsync();
dataGrid.PreparingCellForEdit += DataGrid_PreparingCellForEdit;

var comboBoxColumn = dataGrid.Columns.FirstOrDefault(x => x.Tag.Equals("Mountain")) as DataGridComboBoxColumn;
var comboBoxColumn = dataGrid.Columns.FirstOrDefault(x => x.Tag?.Equals("Mountain") == true) as DataGridComboBoxColumn;
if (comboBoxColumn != null)
{
comboBoxColumn.ItemsSource = await viewModel.GetMountains();
Expand Down Expand Up @@ -111,6 +112,15 @@ public async void OnXamlRendered(FrameworkElement control)
}
}

private void DataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
if (e.Column is DataGridTemplateColumn column && (string)column?.Tag == "First_ascent" &&
e.EditingElement is CalendarDatePicker calendar)
{
calendar.IsCalendarOpen = true;
}
}

private void DataGrid_LoadingRowGroup(object sender, DataGridRowGroupHeaderEventArgs e)
{
ICollectionViewGroup group = e.RowGroupHeader.CollectionViewGroup;
Expand Down
10 changes: 9 additions & 1 deletion Microsoft.Toolkit.Uwp.SampleApp/SamplePages/XamlOnlyPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
<ui:AttachedDropShadow x:Key="AttachedDropShadow" />
<controls:DropShadowPanel x:Key="DropShadowPanel"
ui:Effects.Shadow="{StaticResource AttachedShadow}" />
<controls:DataGrid x:Key="DataGrid">
<controls:DataGrid.Columns>
<controls:DataGridCheckBoxColumn />
<controls:DataGridComboBoxColumn />
<controls:DataGridTemplateColumn />
<controls:DataGridTextColumn />
</controls:DataGrid.Columns>
</controls:DataGrid>
</Page.Resources>

<Grid>
Expand Down Expand Up @@ -90,4 +98,4 @@
</interactions:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Grid>
</Page>
</Page>
13 changes: 12 additions & 1 deletion Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5729,6 +5729,7 @@ private void DataGrid_LostFocus(object sender, RoutedEventArgs e)
{
bool focusLeftDataGrid = true;
bool dataGridWillReceiveRoutedEvent = true;
DataGridColumn editingColumn = null;

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

if (focusLeftDataGrid)
if (this.EditingRow != null && this.EditingColumnIndex != -1)
{
editingColumn = this.ColumnsItemsInternal[this.EditingColumnIndex];

if (focusLeftDataGrid && editingColumn is DataGridTemplateColumn)
{
dataGridWillReceiveRoutedEvent = false;
}
}

if (focusLeftDataGrid && !(editingColumn is DataGridTemplateColumn))
{
this.ContainsFocus = false;
if (this.EditingRow != null)
Expand Down