C# 全选事件:WPF Datagrid
我正在使用WPF数据网格。在数据网格中,用户具有列标题和行标题 当列标题和行标题都可见时,在左上角有一个小的方形部分可用。(列标题和行标题相交的左上角的横截面。)当我们单击它时,它会选择数据网格中的所有单元格。有什么活动吗?如果不是的话,我们如何才能捕捉到那个事件。请引导我 如果您需要有关此问题的任何其他信息,请务必告诉我 问候,,C# 全选事件:WPF Datagrid,c#,wpf,wpfdatagrid,C#,Wpf,Wpfdatagrid,我正在使用WPF数据网格。在数据网格中,用户具有列标题和行标题 当列标题和行标题都可见时,在左上角有一个小的方形部分可用。(列标题和行标题相交的左上角的横截面。)当我们单击它时,它会选择数据网格中的所有单元格。有什么活动吗?如果不是的话,我们如何才能捕捉到那个事件。请引导我 如果您需要有关此问题的任何其他信息,请务必告诉我 问候,, Priyank这不是一个很好的解决方案,但您可以处理“SelectionChanged”之类的事件,并检查所选项目的数量是否等于数据源中的项目数量数据网格处理路由命
Priyank这不是一个很好的解决方案,但您可以处理“SelectionChanged”之类的事件,并检查所选项目的数量是否等于数据源中的项目数量数据网格处理路由命令ApplicationCommand.SelectAll,因此,如果网格具有焦点,您可以按Ctrl-a或单击角点按钮,所有单元格都被选中。 通过在xaml中添加CommandBinding,您可以自己处理此命令:
<DataGrid x:Name="dataGrid" .../>
<DataGrid.CommandBindings>
<CommandBinding Command="ApplicationCommands.SelectAll" Executed="SelectAll_Executed"/>
</DataGrid.CommandBindings>
但是,路由命令只能有一个处理程序,因此默认情况下,添加此处理程序将阻止select all在datagrid中工作。
因此,您需要在处理程序中调用SelectAll
private void SelectAll_Executed(object sender, ExecutedRoutedEventArgs e)
{
Debug.WriteLine("Executed");
dataGrid.SelectAll();
}
我更喜欢避免在视图中使用代码隐藏,因此我这样做: 左上角的复选框用于选择/取消选择全部 解决方案由两部分组成:附加属性和特殊XAML结构: 1) 。附加属性:
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:
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));
}
<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>