博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF 中如何使得DataGrid的Column有鼠标点击相应
阅读量:4462 次
发布时间:2019-06-08

本文共 2246 字,大约阅读时间需要 7 分钟。

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.

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

|
|
 
 
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: 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? –    
1
 
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: 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. –    
 
np Jeff, thank you once again for your help. –    

转载于:https://www.cnblogs.com/norsd/p/6359341.html

你可能感兴趣的文章
团队冲刺第六天
查看>>
integer promotion
查看>>
怎么处理系统蓝屏后提示代码0x000000d1的错误?
查看>>
技术分享:如何在PowerShell脚本中嵌入EXE文件
查看>>
ThreadUtil 多线程处理List,回调处理具体的任务
查看>>
浅析C#中的Attribute
查看>>
【转载】String和StringBuffer的区别,以及StringBuffer的常用方法介绍
查看>>
mysql tp5 find_in_set写法
查看>>
SQL练习之求解填字游戏
查看>>
2017年11月15日
查看>>
codeforces 949B A Leapfrog in the Array
查看>>
类似懒加载的js功能
查看>>
Mysql的DATE_FORMAT()日期格式转换
查看>>
vue实战教程
查看>>
shiro(三),使用第三方jdbcRealm连接数据库操作
查看>>
夜神模拟器
查看>>
SparkStreaming入门及例子
查看>>
Web应用增加struts2支持
查看>>
java程序——凯撒加密
查看>>
面试题:比较两个数字大小
查看>>