Skip to content

Commit b6e9e1f

Browse files
committed
Fix dotnet#32: SelectAll() is not available when SelectionUnit="Cell"
1 parent ab6caf0 commit b6e9e1f

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

DataGridExtensions/DataGridFilterHost.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ private void DataGrid_Loaded([NotNull] object sender, [NotNull] RoutedEventArgs
185185
headersPresenter?.SetValue(KeyboardNavigation.TabNavigationProperty, KeyboardNavigationMode.None);
186186
}
187187

188-
private void DataGrid_SelectAll(object sender, [NotNull] ExecutedRoutedEventArgs e)
188+
private void DataGrid_SelectAll([CanBeNull] object sender, [NotNull] ExecutedRoutedEventArgs e)
189189
{
190190
e.Handled = true;
191191

192192
if (!_isFilteringEnabled || (DataGrid.Items.Count > 0))
193193
{
194-
if (DataGrid.SelectionMode != DataGridSelectionMode.Single)
194+
if (DataGrid.CanSelectAll())
195195
{
196196
DataGrid.SelectAll();
197197
}
@@ -205,9 +205,9 @@ private void DataGrid_SelectAll(object sender, [NotNull] ExecutedRoutedEventArgs
205205
}
206206
}
207207

208-
private void DataGrid_CanSelectAll(object sender, CanExecuteRoutedEventArgs e)
208+
private void DataGrid_CanSelectAll([CanBeNull] object sender, [NotNull] CanExecuteRoutedEventArgs e)
209209
{
210-
e.CanExecute = (DataGrid.SelectionMode != DataGridSelectionMode.Single) || (DataGrid.Items.Count == 0);
210+
e.CanExecute = DataGrid.CanSelectAll() || (DataGrid.Items.Count == 0);
211211
}
212212

213213
private void Columns_CollectionChanged([NotNull] object sender, [NotNull] NotifyCollectionChangedEventArgs e)

DataGridExtensions/ExtensionMethods.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,19 @@ public static IList<DataGridCellInfo> GetVisibleSelectedCells([CanBeNull] this D
237237
.ToArray();
238238
}
239239

240+
/// <summary>
241+
/// Determines whether it's safe to call "DataGrid.SelectAll()".
242+
/// </summary>
243+
/// <param name="dataGrid">The data grid.</param>
244+
/// <returns>
245+
/// <c>true</c> if it's safe to call "DataGrid.SelectAll(); otherwise, <c>false</c>.
246+
/// </returns>
247+
public static bool CanSelectAll([NotNull] this DataGrid dataGrid)
248+
{
249+
return (dataGrid.SelectionMode != DataGridSelectionMode.Single && dataGrid.SelectionUnit != DataGridSelectionUnit.Cell);
250+
}
251+
252+
240253
[NotNull, ItemCanBeNull]
241254
private static IEnumerable<T> Repeat<T>([NotNull, ItemCanBeNull] ICollection<T> source, int count)
242255
{

DataGridExtensionsSample/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
</TextBlock>
145145
</DockPanel>
146146
<DataGrid ItemsSource="{Binding Source={StaticResource SortedItems}}" AutoGenerateColumns="False" FrozenColumnCount="2"
147+
SelectionUnit="Cell"
147148
dgx:DataGridFilter.GlobalFilter="{Binding ExternalFilter}"
148149
dgx:DataGridFilter.IsAutoFilterEnabled="True"
149150
dgx:Tools.ApplyInitialSorting="True"

packages/repositories.config

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)