que prefieren evitar el uso de un código detrás de las vistas, así que hemos hecho de esta manera:
Es CheckBox en la esquina superior izquierda que seleccionar/deseleccionar todos .
La solución se construyen a partir de 2 partes: Propiedades adjunto y especial estructura XAML:
1). propiedades adjuntos:
public class DataGridSelectAllBehavior
{
public static bool GetValue(DependencyObject obj)
{
return (bool)obj.GetValue(ValueProperty);
}
public static void SetValue(DependencyObject obj, bool value)
{
obj.SetValue(ValueProperty, value);
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.RegisterAttached("Value", typeof(bool), typeof(DataGridSelectAllBehavior), new PropertyMetadata(false,
(o, e) =>
{
var dg = DataGridSelectAllBehavior.GetDataGrid(o);
CheckBox checkBox = o as CheckBox;
if (checkBox.IsChecked == true)
{
dg.SelectAll();
}
else
{
dg.UnselectAll();
}
}));
public static DataGrid GetDataGrid(DependencyObject obj)
{
return (DataGrid)obj.GetValue(DataGridProperty);
}
public static void SetDataGrid(DependencyObject obj, DataGrid value)
{
obj.SetValue(DataGridProperty, value);
}
public static readonly DependencyProperty DataGridProperty =
DependencyProperty.RegisterAttached("DataGrid", typeof(DataGrid), typeof(DataGridSelectAllBehavior), new PropertyMetadata(null));
}
2) XAML:
<DataGrid ItemsSource="{Binding PendingChanges}"
AutoGenerateColumns="False"
IsReadOnly="True"
SelectionMode="Extended">
<i:Interaction.Behaviors>
<behaviors:MultiSelectGridSelectedItemsBehavior SelectedItems="{Binding SelectedPendingChanges, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</i:Interaction.Behaviors>
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding Path=IsSelected,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}}">
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate>
<CheckBox
behaviors:DataGridSelectAllBehavior.Value="{Binding IsChecked,RelativeSource={RelativeSource Self}}"
behaviors:DataGridSelectAllBehavior.DataGrid="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}}"/>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
<DataGridTextColumn Header="Name"
Width="Auto"
Binding="{Binding Name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="State"
Width="Auto"
Binding="{Binding State, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Folder"
Width="*"
Binding="{Binding ParentFolderPath, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
</DataGrid>