在做的一个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;
  • 1

再附上一个效果图 

在WPF的DataGrid中对行添加单击事件的更多相关文章

  1. c#datagrid的每行的单击事件

    需要一个帮助类 using System; using System.Net; using System.Windows; using System.Windows.Controls; using S ...

  2. 在WPF的DATAGRID中快速点击出现在ADDNEW或EDITITEM事务过程不允许DEFERREFRESH

    原文 在WPF的DATAGRID中快速点击出现在ADDNEW或EDITITEM事务过程不允许DEFERREFRESH 在项目中关于DataGrid的遇到过一些问题,其中是关于迁入CheckBox的双向 ...

  3. dcef3 为按钮添加单击事件

    http://ju.outofmemory.cn/entry/78083 dcef3 为按钮添加单击事件 BccSafe's Blog 2014-06-01 3706 阅读 DOM 演示dcef3操作 ...

  4. wpf 获取datagrid中模板中控件

    //获取name为datagrid中第三列第一行模板的控件 FrameworkElement item = dataGrid.Columns[].GetCellContent(dataGrid.Ite ...

  5. 对于多个button要在同一个监听器中实现自己的单击事件的方法小诀窍。

    在网上的一些教程中往往是把一个button加入多个监听器,却非常少有人会把多个button在同一个监听器中去实现他们的单击事件,并且这杨的事实上是非常有用的,比方说在制作一个简单的计算器是就须要0-9 ...

  6. hibernate左连接查询时在easyUI的dataGrid中有些行取值为空的解决办法

    1 当使用left join左连连接,sql语句为 select t from SecondPage t left join t.rightNavbar n where 1=1 页面中出现了部分空行的 ...

  7. 如何获得DataGrid中某行某列的对象

    假如某行是 Xm_struct x = this.Brow.SelectedItem as Xm_struct;则下面分别是第5和第七列的对象 TextBlock Ddjs = this.Brow.C ...

  8. WPF拖动DataGrid中的数据到ListBox

    1.效果图: 2.XAML <Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.m ...

  9. WPF dataGrid中的check的改变事件

    关于datagrid信息: <DataGridTemplateColumn Header="备注"> <DataGridTemplateColumn.CellTe ...

随机推荐

  1. python冒泡算法

    array = [1,2,3,6,5,4,28,2,1,9,3,0,200,2,9,3,2,98,33,988,22,0,223,0,33,78,222,88,32,0,238,883,2,0,23] ...

  2. 《The Story of My Life》Introductiom - Historical and Literary Context - Education of the Deaf and Blind

    At the time the Story of My Life was published, the idea of a disabled person as an active member of ...

  3. hibernate 1 + N 问题解决

    1+N就是在hibernate中第一次查一个所需要的表的内容,他会把别的相关表的内容也查询一遍.   解决办法有三种: 1,设置LAZY. 2,借鉴createCriteria的查询语句,from T ...

  4. Linux-《Linux命令行与shell脚本编程大全》阅读笔记

    1.内核负责的主要功能 系统内存管理.软件程序管理.硬件设备管理.文件系统管理 2.GNU工具链 Linux内核是系统的核心,控制着内存.程序和硬件等是如何与对方交互的.内核还需要工具链来执行一些标准 ...

  5. CSS3图片折角效果

    本篇文章由:http://xinpure.com/css3-picture-angle-effect/ 图片折角效果主要是通过设置 border 属性实现的效果 效果预览 效果解析 假设我们将一个元素 ...

  6. Could not create and/or set value back on to object .

    严重: Error building beanorg.springframework.beans.factory.UnsatisfiedDependencyException: Error creat ...

  7. Linux命令-网络命令:lastlog

    last 显示所有用户最后登录信息(会显示系统用户) last -u 只看某一个用户wangyunpeng的最后登录信息 last -u 查看系统用户root的最后登录信息 root用户的ID是0.从 ...

  8. 贪吃蛇easyx版本

    这学期学了图形交互学,三个星期下来,突然意识到已经可以用c++写一个贪吃蛇了. 于是就用了两天写了这个小游戏. 其中一天写了核心代码,半天找核心代码中的bug,还有半天进行了界面及操作的优化. 但是有 ...

  9. Machine-wide Progress Telerik Fiddler installation has been found at ...Please, use that one or uninstall it ...

    问题描述:无法安装Fiddle,提示已有Fiddle,但是却找不到. 打开Fiddle,提示“Machine-wide Progress Telerik Fiddler installation ha ...

  10. mybatis中分页查询

    1 如果在查询方法中有多个参数,可以使用map对象将所有数据都存储进去.比如分页查询,需要用到两个参数,可以将这两个参数包装到map中. 例子:分页查询 dao层方法 public List<S ...