私はビューでの背後にあるコードの使用を避けることを好むので、私はそれをこのようにやった:
それはすべての選択/非選択し、左上隅にチェックボックスであります。添付プロパティと特別XAML構造:
1)
溶液を2つの部分から構築されます。添付プロパティ:
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>