在做的一个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. 如何在eclipse查看jdk源码(src.zip)

    在eclipse编写代码的过程中,有时候想点进去看看jdk的源码,了解下里面具体的实现.在没有任何配置的情况下,应该是看不到源码的. 其实只需要把jdk安装目录下的src.zip压缩包添加到eclip ...

  2. java中关于冒泡排序算法的学习。

    在编程语言的学习中,排序算法在学习过程中是必须要掌握的,特别在新手的学习中,我们应该学会这些算法,本篇先介绍冒泡算法. 冒泡排序 设有一组待排序的数据: 3 2 4 5 7 1 我们需要使用冒泡排序来 ...

  3. 即将要被淘汰的兼容之--CSS Hack

    css hack 条件注释法只在IE下生效<!--[if IE]>这段文字只在IE浏览器显示<![endif]-->只在IE6下生效<!--[if IE 6]>这段 ...

  4. Eclipse plug-in startup

    Plug-in Startup !SESSION 2013-09-02 16:28:29.546 -----------------------------------------------ecli ...

  5. 微信公众平台开发——helloworld

    威信公众平台有两种模式:编辑模式 和 开发模式. 普通的功能可以通过编辑模式来搞定.开发模式具有更多的功能.让我们来使用开发模式开发helloword吧 步骤如下: 1.先注册一个公众号(https: ...

  6. 获取当前事件对象及this的用法

    js <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta h ...

  7. Windows Host 文件

    Windows XP Home / Windows 7/ Windows Server 2008 c:\windows\system32\drivers\etc\hosts 如果碰到Localhost ...

  8. 【转载】#344 - Hidden Base Class Member Is Invoked Based on Declared Type of Object

    When you use the new modifier to hide a base class method, it will still be called by objects whose ...

  9. IOS Get请求(请求服务器)

    @interface HMViewController () <NSURLConnectionDataDelegate> @property (weak, nonatomic) IBOut ...

  10. RHEL/CentOS 6.x使用EPEL6与remi的yum源安装MySQL 5.5.x

    PS:如果既想获得 RHEL 的高质量.高性能.高可靠性,又需要方便易用(关键是免费)的软件包更新功能,那么 FedoraProject 推出的 EPEL(Extra Packages for Ent ...