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 ...
随机推荐
- 1月12日内容总结——文件和文件索引、链接、系统时间、克隆、定时任务、paramiko模块、公钥私钥、paramiko代码封装
目录 一.文件相关信息 二.文件索引信息 三.链接信息 四.系统时间 五.机器克隆 六.定时任务 七.paramiko模块 八.公钥私钥 九.paramiko其他操作 十.代码封装 十一.面试题回忆 ...
- P24_wxss - wxss与css的关系
WXSS 模板样式 什么是 WXSS WXSS (WeiXin Style Sheets)是一套样式语言,用于美化 WXML 的组件样式,类似于网页开发中的 CSS. WXSS 和 CSS 的关系 W ...
- P11_组件-button和image组件的基本用法
其它常用组件 button 按钮组件 功能比 HTML 中的 button 按钮丰富 通过 open-type 属性可以调用微信提供的各种功能(客服.转发.获取用户授权.获取用户信息等) image ...
- Containers feature is disabled. Enable it using the PowerShell script (in an administrative PowerShe
1.问题如题如下图 2.解决办法 以管理员身份运行Window Terminal(Windows PowerShell),输入图上下方所示命令 Enable-WindowsOptionalFeatur ...
- 了解舵机以及MG996R的控制方法
了解舵机以及MG996R的控制方法 1.舵机基础知识: 舵机是遥控航空.航天模型控制动作,改变方向的重要组成部件,舵机是一种位置(角度)伺服的驱动器. 舵机主要适用于那些需要角度不断变化并可以保持的控 ...
- 流量加密之:使用Openssl反弹加密shell
流量加密之:使用Openssl反弹加密shell 目录 流量加密之:使用Openssl反弹加密shell 1 背景 2 生成SSL证书 3 Linux平台下 3.1 在Kali上使用 OpenSSL ...
- 常见数据库mysql、oracle和DB2中is null 和 =null 的区别
问题背景:前段时间我在测试过程中上传一个文件,文件内容要求判断为空,结果出现了报错,跟踪原因发现是开发误将oracle中对null的判断方式写成了=null,下面梳理一下不同数据库对该问题的处理方法: ...
- flutter系列之:Navigator的高级用法
目录 简介 named routes 给named route传参数 从Screen返回值 向Screen传值 总结 简介 上篇文章我们讲到了flutter中navigator的基本用法,我们可以使用 ...
- 在vue-element-admin模板中去掉tui-editor
先删除package.json的"tui-editor": "1.3.3",再安装依赖,不然会报错 1.修改package.json 删除包括tui-edito ...
- Canvas:绘制多边形
前言 CanvasRenderingContext2D 没有提供绘制多边形的函数,所以只能由我们自己来实现绘制多边形的函数.以六边形为基础,需要用到三角函数:sin 和 cos. 点 A 坐标 (一) ...