如何在单击数据网格中的复选框列标题时显示弹出菜单?
我在 WPF Windows 应用程序中使用 WPF 工具包中的数据网格。 它的行标题中有一个数据模板,它创建一个用于选择行的复选框列。
如果单击复选框列的标题行(网格中最左上角的单元格),它将选中网格中的所有复选框,从而选择所有行。
xaml 中的相关部分
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" >
<toolkit:DataGrid Name="dataGrid" ItemsSource="{Binding}"
AutoGenerateColumns="True" SelectionMode="Extended" CanResizeRows="False">
<toolkit:DataGrid.RowHeaderTemplate>
<DataTemplate>
<Grid>
<CheckBox IsChecked="{
Binding Path=IsSelected,
Mode=TwoWay,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type toolkit:DataGridRow}}}"
/>
</Grid>
</DataTemplate>
</toolkit:DataGrid.RowHeaderTemplate>
</toolkit:DataGrid>
</Window>
现在我想知道如何处理此点击。 我计划处理这个问题并显示弹出菜单。
我应该为此连接哪个控件的单击事件?
答案: 使用 SelectAllCommand 来处理事件,如下所示(示例)
datagrid.CommandBindings.Add(new CommandBinding(
Microsoft.Windows.Controls.DataGrid.SelectAllCommand,
OnSelectAll, CanExecuteSelectAll));
public void OnSelectAll(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("All Selected");
}
public void CanExecuteSelectAll(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
Console.WriteLine("Can I execute?");
}
I am using the datagrid from WPF toolkit in a WPF windows application.
It has a datatemplate in the rowheader which creates a checkbox column used for selecting the rows.
If you click on the header row of the checkbox column (the top leftmost cell in the grid), it will check all the checkboxes in the grid thereby selecting all the rows.
Relevant portions from the xaml
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" >
<toolkit:DataGrid Name="dataGrid" ItemsSource="{Binding}"
AutoGenerateColumns="True" SelectionMode="Extended" CanResizeRows="False">
<toolkit:DataGrid.RowHeaderTemplate>
<DataTemplate>
<Grid>
<CheckBox IsChecked="{
Binding Path=IsSelected,
Mode=TwoWay,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type toolkit:DataGridRow}}}"
/>
</Grid>
</DataTemplate>
</toolkit:DataGrid.RowHeaderTemplate>
</toolkit:DataGrid>
</Window>
Now I want to know how to handle this click. I plan to handle that and show a popup menu instead.
Which control's click event should I wire for this?
Answer:
using SelectAllCommand to handle the events like so (example)
datagrid.CommandBindings.Add(new CommandBinding(
Microsoft.Windows.Controls.DataGrid.SelectAllCommand,
OnSelectAll, CanExecuteSelectAll));
public void OnSelectAll(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("All Selected");
}
public void CanExecuteSelectAll(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
Console.WriteLine("Can I execute?");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果展开 DataGrid ControlTemplate,您可以看到有一个绑定到 SelectAllCommand 的按钮。
您可以覆盖该按钮的命令来执行您自己的逻辑,甚至可以用不同的控件替换该按钮。
If you expand the DataGrid ControlTemplate you can see that there is a Button which is bound to the SelectAllCommand.
You can override that Button's Command to perform your own logic or even replace the button with a different control.