在做的一个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. intellijidea课程 intellijidea神器使用技巧 3-2 livetemplate

    创建livetemplate分组: ctrl shift a ==> live templates ==> + ==> templates group 创建livetemplate模 ...

  2. 基于nodejs的DNS查询工具

    开始这个实例之前,我们简单谈一下Node.js吧,Node.js是一个由JavaScript书写而成的强大Web开发框架,它让开发强壮的.伸缩性良好的服务器端Web应用变得更加简单.容易.这种技术诞生 ...

  3. Python常用模块(一)

    一.time模块 time模块提供各种操作时间的函数 时间三种格式 1.时间戳 以1970年1月1日 00:00:00开始的秒数 2.本地时间 localtime,表示计算机当前的时间 3.UTC世界 ...

  4. 背景图片之background的用法

    常用的background背景属性有: background-color 设置颜色作为对象背景颜色background-image 设置图片作为背景图片background-repeat 设置背景平铺 ...

  5. 在CentOS上配置Tomcat服务脚本

    #!/bin/bash # chkconfig: - 85 15 # description: Tomcat Server basic start/shutdown script # processn ...

  6. Linux 两组信号对比

    博客逐步迁移到,独立博客,原文地址 http://www.woniubi.cn/two_groups_signal_difference/ 之前看信号的时候,没有太注意不同信号的对比.今天再次看到的时 ...

  7. Android中关于XML的一个小问题——使用XML输出“<”错误的问题

    在 XML 中,有 5 个预定义的实体引用: < < 小于 > > 大于 & & 和号 &apos; ' 单引号 " " 引号 注释 ...

  8. Ubuntu 16.10 安装mysql

    打开终端 sudo apt update 完成后 sudo apt install mysql-server 中间会提示设置root 账户的密码 有的文章提到 还要 install mysql-cli ...

  9. Linux命令之查看内存和CPU消耗命令TOP使用

    1)输入top ,按enter键,即可查看服务器内存消耗情况 注意:其中PID表示进程号 :%cpu表示cpu消耗情况:%M表示内存消耗情况:通常在做性能测试的时候用到该命令: 默认为实时刷新:按s键 ...

  10. "ssllabs" website and "testssl" website

    "https://www.ssllabs.com" could scan your server and give results about the weakness of yo ...