在做的一个c#的项目中发现Datagrid没办法直接对鼠标单击进行响应,

调用MouseDown事件也需要点击某一行第二次才能响应。

所以借助EventSetter来简单的实现了一个。

界面部分的代码

        <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="10,38,0,0"
VerticalAlignment="Top" Height="257" FontSize="13.333"
AutoGenerateColumns="False" AlternationCount="2" CanUserAddRows="False"
MinWidth="504" Width="513" SelectionUnit="FullRow">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="GotFocus" Handler="Item_GotFocus"/>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridCheckBoxColumn />
<DataGridTextColumn Header="歌曲名" Width="200" Binding="{Binding Path=Title}" IsReadOnly="True"/>
<DataGridTextColumn Header="艺术家" Width="127" Binding="{Binding Path=Artist}" IsReadOnly="True"/>
<DataGridTextColumn Header="专辑" Width="140" Binding="{Binding Path=Album}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>

对应的c#的代码

        private void Item_GotFocus(object sender, RoutedEventArgs e)
{
var item = (DataGridRow)sender;
FrameworkElement objElement = dataGrid.Columns[0].GetCellContent(item);
if (objElement != null)
{
CheckBox objChk = (CheckBox)objElement;
objChk.IsChecked = !objChk.IsChecked;
}
}

附上用mousedown事件的代码

        private void DataGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var se = dataGrid.SelectedItem;
FrameworkElement objElement = dataGrid.Columns[0].GetCellContent(se);
if (objElement != null)
{
CheckBox objChk = (CheckBox)objElement;
objChk.IsChecked = !objChk.IsChecked;
}
}
}

需要在这个界面的构造函数中添加

 dataGrid.MouseDown += DataGrid_MouseDown;

WPF Datagrid对鼠标单击进行响应,借助EventSetter的更多相关文章

  1. WPF DataGrid RowDetailsTemplate 鼠标滚动通知到 DataGrid 滚动

    前言:上次做了数据驱动UI虽然已经实现,但是在明细中鼠标滚动并不能带动外部 DataGrid 滚动条滚动,上文地址  https://www.cnblogs.com/luguangguang/p/14 ...

  2. WPF DataGrid中鼠标双击某一列,弹出窗体作为(增加、修改、详细)按钮的快捷键。

    跟触发器行为有关,什么是触发器什么是行为,百度其他人写的乱七八糟的,我并不能看懂.在此先强行记忆,后知后觉,再回来理解. <i:Interaction.Triggers> <i:Ev ...

  3. 编写 WPF DataGrid 列模板,实现更好的用户体验

    Julie Lerman 下载代码示例 最近我在为一个客户做一些 Windows Presentation Foundation (WPF) 方面的工作. 虽然我提倡使用第三方工具,但有时也会避免使用 ...

  4. WPF DataGrid 样式设置

    隔行换色,鼠标单击,悬浮样式都有,其具体效果如图 1 所示. 图 1 WPF DataGrid 样式设置效果图 其中: 界面设计代码下所示 ? + 查看代码 1 2 3 4 5 6 7 8 9 10 ...

  5. WPF DataGrid 样式分享

    原文:WPF DataGrid 样式分享 隔行换色,鼠标单击,悬浮样式都有 先看效果: 代码: <DataGrid AutoGenerateColumns="False" N ...

  6. WPF DataGrid 控件的运用

    WPF DataGrid 控件的运用 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-23 参考: King Cobra 博客 ...

  7. WPF datagrid/gridcontrol 中选中多行,复制粘贴到excel或其他文本编辑器中

    wpf中 data grid 开启自带的选中,然后复制,可以到excel中直接粘贴,在某些业务场景中很实用,方便.开启也很简单: SelectionMode="Row" 加上这个, ...

  8. WPF DataGrid常用属性记录

    WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...

  9. 【2016.3.30项目技术记录】]VS2010自动生成MFC单文档框架程序的修改:去除属性框,在CViewTree类中添加鼠标单击响应

    转自http://blog.csdn.net/yanfeiouc2009/archive/2010/06/07/5653360.aspx 手头上有个东西要用到单文档,由于想省事,直接用VS2010做了 ...

随机推荐

  1. poj 3140 树形去边差异最小

    http://poj.org/problem?id=3140 依然是差异最小问题,不过这次是去边.思路是这样的,先记录每个点的子节点个数,然后遍历每个边. 有两个问题要注意: abs可能会出编译适配问 ...

  2. CSS的相对定位和绝对定位(position)

    什么是定位呢? 定位(position),故名思议,就是确定元素在页面中的位置. CSS的常用定位有两种,一种是相对定位,一种是绝对定位. 下面我们看例子 <html> <head& ...

  3. 从零开始的全栈工程师——js篇(作用域 this 原型笔试题练习)

    作用域 // 1. fn() function fn () { console.log(12) } var as = function () { console.log(45) } // 2. var ...

  4. oracle笔记2-多表查询和子查询

    --查询出当前用户下的所有表 select table_name from user_tables; --执行顺序原则:  from  where group by  having   select  ...

  5. CSS3的Animation

    1.animation-name :动画名    2.animation-duration:时间    3.animation-delay:延时    4.animation-iteration-co ...

  6. Win7安装软件,装到microsoft.vc90.crt时卡住的解决办法

    在安装某些程序的时候,可能会出现下列提示:an error occured during the installation of assembly ‘microsoft.vc90.crt,versio ...

  7. 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01

    本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...

  8. tomcat的相关使用

    tomcat服务器是apache下非常优秀的一款web服务器,当今的互联网企业中90%左右的中小型企业使用的都是tomcat.tomcat在部署项目时有很多很多的解决方案,这些你都清楚吗? 1.同一个 ...

  9. Struts2_用ModelDriven接收参数

    通过实现 ModelDriven 接口来接收请求参数,这种方法用的比较少,一般还是用前两种. 请求: <a href="user/user!add?name=xiaoer&ag ...

  10. DEM精度评价自动化系统的成果展示

     程序员:左正康 完成时间:2013/12/3 系统开发背景: 原始的DEM精度评价方法:采用ArcGIS结合Excel的方式完成DEM的精度评价.具体操作是:利用ArcGIS工具箱中的创建TIN,T ...