WPF 中如何使得DataGrid的Column有鼠标点击相应

http://stackoverflow.com/questions/5895803/how-do-i-capture-click-events-on-a-datagrid-column-headers


The headers are just buttons. Like any button, you can register to the Click event to capture those clicks. Just set a style targeting DataGridColumnHeader and add a Click event 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.

<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 ColumnHeaderStyle property. I think it would be a better idea to apply the style through this property instead.

<DataGrid>
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <EventSetter Event="Click" Handler="columnHeader_Click" />
        </Style>
    </DataGrid.ColumnHeaderStyle>
</DataGrid>
share|edit|flag
 
 
Thank you very much Jeff, I had never used EventSetter before, very handy. I have a followup question, do you know if it's possible to get the style behaviour when CanUserSortColumns="False" so the headers respond to mouse hover and show the sort direction, or will I need to restyle these myself? –  Brett Ryan May 6 '11 at 3:05
 
@Brett: Sorry, could you rephrase that? I'm not sure I understand what you are asking. It sounds to me like you want to get the templates associated with these headers when sorting is disabled? –  Jeff Mercado May 6 '11 at 3:32
1 upvote
  flag
Thanks Jeff, Sorry for the ambiguity. What I'm trying to do is provide my own sorting logic, with from your help I have now achieved this, however I of course have to set CanUserSortColumns to False otherwise the control will try to perform the sort false, however I want the nice "hover" affect when moving the cursor over the column header, and would like the "up/down" arrow indicating sort direction (though the latter is less important). Your help is very much appreciated. –  Brett Ryan May 6 '11 at 4:04
 
@Brett: I'm pretty sure you can, I just don't remember how to be honest. Though I will be investigating. You might want to post that as another question. –  Jeff Mercado May 6 '11 at 5:10
 
np Jeff, thank you once again for your help. –  Brett Ryan May 6 '11 at 8:37

posted on   norsd  阅读(840)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示