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>
answered May 5 '11 at 10:51
Jeff
Mercado


38.8k55497
 
 
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

WPF 中如何使得DataGrid的Column有鼠标点击相应的更多相关文章

  1. WPF中。。DataGrid 实现时间控件和下拉框控件

    DatePicker 和新的 DataGrid 行 用户与 DataGrid 中日期列的交互给我造成了很大的麻烦. 我通过将一个 Data Source 对象拖动到 WPF 窗口上,创建了一个 Dat ...

  2. WPF中TreeView单击展开其子元素以及点击一个元素展开其他元素收起

    TreeView单击展开其子元素: 在WPF的TreeView控件中,要想展开它的子元素,我们必须要鼠标左键点两下或者右键点一下,那么我们怎样实现左键点一下就使它展开呢? Xaml: <Grid ...

  3. WPF中反转3D列表项

    原文:WPF中反转3D列表项 WPF中反转3D列表项                                                         周银辉记得在苹果电脑中有一个很酷的 ...

  4. WPF 中获取DataGrid 模板列中控件的对像

    WPF 中获取DataGrid 模板列中控件的对像 #region 当前选定行的TextBox获得焦点 /// <summary> /// 当前选定行的TextBox获得焦点 /// &l ...

  5. wpf将表中数据显示到datagrid示例(转)

    原文:http://www.jb51.net/article/47120.htm 这篇文章主要介绍了wpf将表中数据显示到datagrid示例,需要的朋友可以参考下 a.在.xaml文件中拖入一个da ...

  6. WPF中datagrid不显示滚动条问题

    WPF中DataGrid是自带滚动条的,当内容显示不下时可以使用滚动条显示.但是,开发中遇到了DataGrid一直不显示滚动条.之前也曾遇到过同类问题,再次经过查找,发现是StackPanel的原因, ...

  7. WPF中DataGrid中的DataGridCheckBoxColumn用法(全选,全否,反选)

    原文:WPF中DataGrid中的DataGridCheckBoxColumn用法(全选,全否,反选) 前台代码 <DataGrid.Columns> <DataGridCheckB ...

  8. WPF中DATAGRID自定义验证(包含BINDINGGROUP)

    DataGrid在Wpf中的应用是十分广泛的,当你需要表中的信息稍详细的显示出来时,或者我们需要进行某些数据输入时,都有可能采用DataGrid.当然对信息的显示,我们不需要进行验证,但当我们将Dat ...

  9. Working Experience - WPF 中 DataGrid 控件的应用

    问题: 添加控件后, 编辑单元格会出现异常 绑定 ItemsSource 属性后, 更新绑定对象的数据, UI 不刷新 如何显示控件中 ComboBox 类型 解决方法: 绑定 ItemsSource ...

随机推荐

  1. iOS非常全的第三方库

    iOS ● 非常全的三方库.插件.大牛博客等等   github排名:https://github.com/trending, github搜索:https://github.com/search. ...

  2. Go怎么获取当前时间? Go ARM64 vDSO优化之路

    https://mzh.io/ Go ARM64 vDSO优化之路 2018-03-16  | Meng Zhuo 背景 Go怎么获取当前时间?问一个会Go的程序员,他随手就能写这个出来给你. imp ...

  3. linux shell执行远程计算机上的命令或者脚本(ssh)

    大数据平台下经常建立设计多个节点的集群需要统一部署,这就设计到守护进程或者部署脚本在不同节点执行,如果能在master机器上,统一执行脚本,一次性启动整个集群的服务,感觉很nice.因为,分享如下内容 ...

  4. appium(10)-iOS predictate

    iOS predictate It is worth looking at ’-ios uiautomation’ search strategy with Predicates. UIAutomat ...

  5. Struts使用锚

    <a href="exam/ExaminationTrainAction_examTrainDisp#an_${id}"><div id="${id}& ...

  6. Mac终端基本指令,一些实用命令的收集.

    基本命令1.列出文件ls 参数 目录名        例: 看看驱动目录下有什么:ls /System/Library/Extensions参数 -w 显示中文,-l 详细信息, -a 包括隐藏文件 ...

  7. Windows程序设计(0)——编程之前

    Windows程序设计之前 1 做什么 2 解决什么问题 3 有哪些资源 在开始真正的编程之前,需要了解要做的事情是什么,要解决的解决的问题是什么,有哪些资源可以使用. 1 Windows程序设计之前 ...

  8. Appium——解决每次启动时都安装setting和unlock app方法

    找到appium安装目录 C:\Program Files (x86)\Appium\node_modules\appium\lib\devices\android 修改代码,注释掉弹出setting ...

  9. CentOS中文乱码之解决办法

    在学习Linux的过程中,最先碰到的是通过SSH终端连接时发现有乱码出现,使用这篇文章先从这里说起. 在 ssh , telnet 终端中文显示乱码解决办法#vim /etc/sysconfig/i1 ...

  10. MYSQL进阶学习笔记十五:MySQL 的账号权限赋予!(视频序号:进阶_33,34)

    知识点十六:MySQL的账号权限赋予(33) 一.MySQL权限简介 关于mysql的权限简单的理解就是mysql允许你做你全力以内的事情,不可以越界.比如只允许你执行select操作,那么你就不能执 ...