.net 如何在 DataGrid 列标题上捕获“单击”事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5895803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I capture "Click" events on a DataGrid column headers
提问by Brett Ryan
It appears that the DataGridthat comes with .NET 4 does not contain an event for both column and row header clicks. I want to receive events for the column header click as I want to provide my own sorting behaviour and turn off the default sorting, this is because my view-model is a paginated model that will only display 25/50/100 rows at a time, the default sorting will of-course sort the current page only.
看来,DataGrid随.NET 4中不包含列和行标题点击的事件。我想接收列标题单击的事件,因为我想提供我自己的排序行为并关闭默认排序,这是因为我的视图模型是一个分页模型,一次只能显示 25/50/100 行,默认排序当然只会对当前页面进行排序。
Now I could create a new DataGridColumnHeaderstyle which contains a clickable element and and set ColumnHeaderStyle, though this just seems like a pain as I'll have trouble figuring out things like which column it was that got clicked etc.
现在我可以创建一个DataGridColumnHeader包含可点击元素和 set的新样式ColumnHeaderStyle,尽管这看起来很痛苦,因为我很难弄清楚它是哪一列被点击等。
Anyone else come up against this and solved it?
有没有其他人遇到过这个问题并解决了它?
回答by Jeff Mercado
The headers are just buttons. Like any button, you can register to the Clickevent to capture those clicks. Just set a style targeting DataGridColumnHeaderand add a Clickevent handler. Then within the handler, you have access to the header directly via the sender. You could then get the Columnassociated with that header and other information associated with it.
标题只是按钮。与任何按钮一样,您可以注册Click事件以捕获这些点击。只需设置一个样式定位DataGridColumnHeader并添加一个Click事件处理程序。然后在处理程序中,您可以直接通过sender. 然后,您可以获得Column与该标头相关联的信息以及与其关联的其他信息。
<DataGrid>
<DataGrid.Resources>
<Style TargetType="DataGridColumnHeader">
<EventSetter Event="Click" Handler="columnHeader_Click" />
</Style>
</DataGrid.Resources>
</DataGrid>
Then in the code:
然后在代码中:
private void columnHeader_Click(object sender, RoutedEventArgs e)
{
var columnHeader = sender as DataGridColumnHeader;
if (columnHeader != null)
{
// do stuff
}
}
Looking further into the DataGrid, I noticed that there's a ColumnHeaderStyleproperty. I think it would be a better idea to apply the style through this property instead.
进一步查看DataGrid,我注意到有一个ColumnHeaderStyle属性。我认为通过此属性应用样式会更好。
<DataGrid>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<EventSetter Event="Click" Handler="columnHeader_Click" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
回答by Hans-Peter Kalb
It is also possible to do this programmatically:
也可以以编程方式执行此操作:
using System.Windows.Controls.Primitives;
...
public TestWindow()
{
if (TestDataGrid.ColumnHeaderStyle == null)
{
TestDataGrid.ColumnHeaderStyle = new Style(typeof(DataGridColumnHeader));
}
TestDataGrid.ColumnHeaderStyle.Setters.Add(new EventSetter(ButtonBase.ClickEvent,
new RoutedEventHandler(TestDataGrid_HeaderRowClick)));
}
private void TestDataGrid_HeaderRowClick(object sender, RoutedEventArgs e)
{
...
}
Here the method "TestDataGrid_HeaderRowClick" is called, when the use clicks with the left mouse button into the header row.
这里调用方法“TestDataGrid_HeaderRowClick”,当用户用鼠标左键点击标题行时。

