Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “WPF数据网格”;选择全部";按钮-“;取消全选“;也_C#_.net_Wpf_Datagrid_Wpfdatagrid - Fatal编程技术网

C# “WPF数据网格”;选择全部";按钮-“;取消全选“;也

C# “WPF数据网格”;选择全部";按钮-“;取消全选“;也,c#,.net,wpf,datagrid,wpfdatagrid,C#,.net,Wpf,Datagrid,Wpfdatagrid,我想知道是否可以向datagrid左上角的“全选”按钮添加功能,以便它也取消选中所有行?我在一个按钮上附加了一个方法来实现这一点,但是如果我可以从“全选”按钮启动这个方法,将功能保持在视图的同一部分,那将是非常棒的。这个“全选”按钮可以添加代码吗?如果可以,如何才能找到该按钮?我找不到任何示例或建议。好的,经过大量搜索,我找到了如何从Colin Eberhardt处找到按钮,如下所示: 然后我在他的类中扩展了“Grid_Loaded”方法,将事件处理程序添加到按钮中,但请记住首先删除默认的“S

我想知道是否可以向datagrid左上角的“全选”按钮添加功能,以便它也取消选中所有行?我在一个按钮上附加了一个方法来实现这一点,但是如果我可以从“全选”按钮启动这个方法,将功能保持在视图的同一部分,那将是非常棒的。这个“全选”按钮可以添加代码吗?如果可以,如何才能找到该按钮?我找不到任何示例或建议。

好的,经过大量搜索,我找到了如何从Colin Eberhardt处找到按钮,如下所示:

然后我在他的类中扩展了“Grid_Loaded”方法,将事件处理程序添加到按钮中,但请记住首先删除默认的“Select All”命令(否则,在运行我们添加的事件处理程序后,该命令将运行)

//
///处理DataGrid的已加载事件。
/// 
///发送方对象。
///事件参数。
已加载专用静态无效网格(对象发送器、路由目标)
{
DataGrid grid=发送方作为DataGrid;
DependencyObject dep=网格;
//向下导航到可视化树中的按钮
而(!(dep是按钮))
{
dep=VisualTreeHelper.GetChild(dep,0);
}
按钮按钮=dep as按钮;
//应用我们的新模板
ControlTemplate=GetSelectAllButtonTemplate(网格);
button.Template=模板;
button.Command=null;
按钮。单击+=新建路由EventHandler(选择全部单击);
}
/// 
///处理DataGrid的“全选”按钮的单击事件。
/// 
///发送方对象。
///事件参数。
private static void SelectAllClicked(对象发送方、路由目标方)
{
按钮按钮=发送器为按钮;
DependencyObject dep=按钮;
//向上导航可视化树到栅格
而(!(dep是DataGrid))
{
dep=VisualTreeHelper.GetParent(dep);
}
DataGrid grid=dep作为DataGrid;
if(grid.SelectedItems.Count
本质上,如果没有选择任何行,它将“全部选择”,如果没有,它将“全部取消选择”。它的工作原理与您期望的select/unselect all的工作原理非常相似,老实说,我不敢相信他们在默认情况下没有让命令这么做,也许在下一个版本中

希望这对别人有帮助, 干杯
Will

我们可以添加commandbinding来处理selectall事件


请参阅:

谢谢-如果包含“GetSelectAllButtonTemplate()”的定义,那么代码示例将是完整的。
/// <summary>
/// Handles the DataGrid's Loaded event.
/// </summary>
/// <param name="sender">Sender object.</param>
/// <param name="e">Event args.</param>
private static void Grid_Loaded(object sender, RoutedEventArgs e)
{
    DataGrid grid = sender as DataGrid;
    DependencyObject dep = grid;

    // Navigate down the visual tree to the button
    while (!(dep is Button))
    {
        dep = VisualTreeHelper.GetChild(dep, 0);
    }

    Button button = dep as Button;

    // apply our new template
    ControlTemplate template = GetSelectAllButtonTemplate(grid);
    button.Template = template;
    button.Command = null;
    button.Click += new RoutedEventHandler(SelectAllClicked);
}

/// <summary>
/// Handles the DataGrid's select all button's click event.
/// </summary>
/// <param name="sender">Sender object.</param>
/// <param name="e">Event args.</param>
private static void SelectAllClicked(object sender, RoutedEventArgs e)
{
    Button button = sender as Button;
    DependencyObject dep = button;

    // Navigate up the visual tree to the grid
    while (!(dep is DataGrid))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }

    DataGrid grid = dep as DataGrid;

    if (grid.SelectedItems.Count < grid.Items.Count)
    {
        grid.SelectAll();
    }
    else
    {
        grid.UnselectAll();
    }

    e.Handled = true;
}