WPF datagrid双击一整行而不是选中单元格
WPF开发一个工具
需要双击datagrid的某一行显示详细数据并编辑,之前双击行(DatagridRow)每次都跳转到单元格上(DatagridCell)
经验证,需要修改datagrid样式的某几个属性值
datagrid的默认样式,写在App.xaml的资源样式里就行。重点在于最后两个属性的设置,selectionUnit——表示传递整行信息
MVVM模式用obserablecollection绑定到datagrid上,用fullrow即可传递整个某个子项的完整信息
<Style TargetType="{x:Type DataGrid}" >
<Setter Property="Background" Value="White"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="AlternationCount" Value="0"/>
<Setter Property="RowHeaderWidth" Value="0"/>
<Setter Property="SelectionUnit" Value="FullRow"/>
<Setter Property="SelectionMode" Value="Extended"/>
</Style>
datagrid的xaml代码 编辑或者双击某行显示相应的数据,绿色代码是mvvm模式双击命令绑定到后台MVVM的EditCmd上,传递参数就是选中项的某个属性值。SelectedItem.Index——index是我obserablecollection泛型类里的一个属性,可以替换(不是真正的index)
<Datagrid>
<DataGrid.InputBindings>
<MouseBinding
MouseAction="LeftDoubleClick"
Command="{Binding EditCmd}" CommandParameter="{Binding SelectedItem.Index,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}"/>
</DataGrid.InputBindings>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<TextBlock Text="操作"/>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type data:SprayRulerData}" >
<StackPanel Orientation="Horizontal">
<!--<TextBlock Margin="2,0,0,0" Text="" Foreground="#6472B1" />
<TextBlock Text="{Binding Id}" Foreground="#6472B1" Width="20" VerticalAlignment="Center"/>-->
<Button Content="编辑" Background="#F1F19E" Foreground="Black" Command="{Binding DataContext.EditCmd,ElementName=view}" CommandParameter="{Binding Index}" Margin="15,0,0,0" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</Datagrid>
MVVM下的EditCmd代码,双击或编辑按键都能跳转对应的数据窗口
EditCmd = new DelegateCommand<object>(Edit);
///
/// 编辑、更新数据
///
public void Edit(object o)
{
flag = false;
var s = (RMC.SprayRulerIndex)o;
if (o is RMC.SprayRulerIndex)
{
if (RulerModels != null)
{
foreach (var item in RulerModels)
{
if (item.Index == s)
{
rulerSelected = item;
}
}
}
if (rulerSelected != null)
{
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
{
AddSprayView win = new AddSprayView();
((AddSprayViewModel)win.DataContext).EditCkickHandle(this, rulerSelected);
win.Show();
// win.Show();
}));
}
}
}
WPF datagrid双击一整行而不是选中单元格的更多相关文章
- WPF DataGrid 双击行 获得绑定数据
原文:WPF DataGrid 双击行 获得绑定数据 1)增加事件 2)增加对象获取 1)事件代码 Datagrid 增加事件 MouseDoubleClick="dataGrid_Mous ...
- GRIDVIEW多行多列合并单元格(合并列)
GitHub项目地址:https://github.com/mingceng/merge-gridviewcell 去年的时候,我写了两篇文章: GridView多行多列合并单元格(完整代码和例子) ...
- GridView——标题行自适应单元格列宽与选中单元格变色
首先看效果图: 主要实现—— 1.前台GridView代码: <asp:GridView Height="100%" Width="98%" ID=&qu ...
- 复杂的POI导出Excel表格(多行表头、合并单元格)
poi导出excel有两种方式: 第一种:从无到有的创建整个excel,通过HSSFWorkbook,HSSFSheet HSSFCell, 等对象一步一步的创建出工作簿,sheet,和单元格,并添加 ...
- JS遍历表格获取每行数据及每个单元格数据
/** * 遍历表格获取每行数据及每个单元格数据 * @param tableID 表格ID */ function GetTable(tableID) { var milasUrl = {};//新 ...
- 【转】C# DataTable 导出 Excel 进阶 多行表头、合并单元格、中文文件名乱码
本文原创地址:http://blog.csdn.net/ranbolwb/article/details/8083983 ,转载请保留本行. 本例子是上一篇 DataTable 导出 Excel 的进 ...
- 怎样实现在DBGrid中双击选择整行,并且可以多选?谢谢!!
DBGrid1->Options里有个dgMultiSelect,把它设为true就能多选了 先设置DBGrid1->options中dgRowSelect = true, dgMulti ...
- wpf DataGrid 双击获取当前行的控件
<DataGrid Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top& ...
- easyui datagrid动态设置行、列、单元格不允许编辑
Easyui datagrid 行编辑.列编辑.单元格编辑设置 功能: 动态对datagrid 进行行.列.单元格编辑进行设置不允许编辑. 禁用行编辑: 在编辑方法调用前,对选择的行进行判断,如果不允 ...
- c# WPF DataGrid 获取选中单元格信息
private void Dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { Console.Write ...
随机推荐
- WAF Bypass 介绍与实战
前言 WAF是英文"Web Application Firewall"的缩写,中文意思是"Web应用防火墙",也称为"网站应用级入侵防御系统" ...
- TEB学习
官方资料:http://wiki.ros.org/teb_local_planner/Tutorials set up and test Optimization(重要) Inspect optimi ...
- freertos内存pvPortMalloc 和 malloc 区别 ,以及全局变量占用情况
1.FreeRtos占用内存 #define configTOTAL_HEAP_SIZE ( ( size_t ) ( 30 * 1024 ) ) pvPortMalloc是从configTOTAL_ ...
- C++练习2 强制类型转换
const可以把有关的数据定义为常量. const类型可以修饰:对象,指针,引用 使用const_cast为强制类型转换,将常量强制转换非常量. 1 #include <iostream> ...
- BUUCTF-[SUCTF2019]EasySQL
[SUCTF2019]EasySQL 这个连闭合都测试不出来,就有点无从下手了. 额,随手输了一个1;show tables;出来了表名.一通操作之后,点开了别人的wp,学习了一波. 然后知道了题目中 ...
- Python:Excel自动化实践入门篇 甲【留言点赞领图书门票】
*以下内容为本人的学习笔记,如需要转载,请声明原文链接微信公众号「englyf」https://mp.weixin.qq.com/s?__biz=MzUxMTgxMzExNQ==&mid=22 ...
- 基于Ubuntu搭建OpenGL开发环境
1. 引言 笔者这里基于Ubuntu 20.04.3 LTS系统,搭建OpenGL开发环境,主要使用的库有GLFW和GLAD GLFW是一个专门针对OpenGL的C语言库,它提供了一些渲染物体所需的最 ...
- angular 基本操作
1.新建项目(带路由) ng new demo --routing 2.新建组件 统一放到components目录下(文件夹会自动创建) ng g component components/home ...
- nginx部署+将安装包打包到requirements.txt文件中
pip freeze > requirements.txt 将在虚拟环境中安装的包记录到requirements.txt里 详解见https://www.jianshu.com/p/dba8 ...
- VideoMAE Masked Autoencoders are Data-Efficient Learners for Self-Supervised Video Pre-Training概述
0.前言 相关资料: arxiv github 论文解读(知乎,CSDN) 论文基本信息: 领域:视频自监督表示学习 发表时间:NeurIPS 2022(2022.3.23) 1.针对的问题 视频存在 ...