c# WPF DataGrid 获取选中单元格信息
- private void Dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
- {
- Console.WriteLine("start");
- foreach (DataGridCellInfo info in dg.SelectedCells)
- {
- FrameworkElement element = info.Column.GetCellContent(info.Item);
- string str = ((TextBlock)info.Column.GetCellContent(info.Item)).Text;
- Console.WriteLine(str);
- }
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- DataGridCellInfo info = new DataGridCellInfo(dg.Items[], dg.Columns[]);
- Console.WriteLine("start");
- FrameworkElement element = info.Column.GetCellContent(info.Item);
- string str = ((TextBlock)info.Column.GetCellContent(info.Item)).Text;
- Console.WriteLine(str);
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- DataGridCell cell = dg.GetCell(, );
- TextBlock tb = cell.Content as TextBlock;
- Console.WriteLine(tb.Text);
- }
- public static class DataGridExtension
- {
- /// <summary>
- /// 获取DataGrid控件单元格
- /// </summary>
- /// <param name="dataGrid">DataGrid控件</param>
- /// <param name="rowIndex">单元格所在的行号</param>
- /// <param name="columnIndex">单元格所在的列号</param>
- /// <returns>指定的单元格</returns>
- public static DataGridCell GetCell(this DataGrid dataGrid, int rowIndex, int columnIndex)
- {
- DataGridRow rowContainer = dataGrid.GetRow(rowIndex);
- if (rowContainer != null)
- {
- DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
- DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
- if (cell == null)
- {
- dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]);
- cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
- }
- return cell;
- }
- return null;
- }
- /// <summary>
- /// 获取DataGrid的行
- /// </summary>
- /// <param name="dataGrid">DataGrid控件</param>
- /// <param name="rowIndex">DataGrid行号</param>
- /// <returns>指定的行号</returns>
- public static DataGridRow GetRow(this DataGrid dataGrid, int rowIndex)
- {
- DataGridRow rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
- if (rowContainer == null)
- {
- dataGrid.UpdateLayout();
- dataGrid.ScrollIntoView(dataGrid.Items[rowIndex]);
- rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
- }
- return rowContainer;
- }
- /// <summary>
- /// 获取父可视对象中第一个指定类型的子可视对象
- /// </summary>
- /// <typeparam name="T">可视对象类型</typeparam>
- /// <param name="parent">父可视对象</param>
- /// <returns>第一个指定类型的子可视对象</returns>
- public static T GetVisualChild<T>(Visual parent) where T : Visual
- {
- T child = default(T);
- int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
- for (int i = ; i < numVisuals; i++)
- {
- Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
- child = v as T;
- if (child == null)
- {
- child = GetVisualChild<T>(v);
- }
- if (child != null)
- {
- break;
- }
- }
- return child;
- }
- }
c# WPF DataGrid 获取选中单元格信息的更多相关文章
- WPF DataGrid 获取选中 一行 或者 多行
WPF中DataGrid使用时,需要将其SelectedItem转换成DataRowView进行操作 然而SelectedItem 与SelectedItems DataGrid的SelectionU ...
- 【转】WPF DataGrid 获取选中的当前行某列值
方法一:DataRowView mySelectedElement = (DataRowView)dataGrid1.SelectedItem; string result = mySelectedE ...
- WPF DataGrid 获取选中的当前行某列值
方法一: DataRowView mySelectedElement = (DataRowView)dataGrid1.SelectedItem; ]ToString(); 方法二: var a = ...
- WPF:获取DataGrid控件单元格DataGridCell
转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...
- WPF备忘录(3)如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter
一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...
- 如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter
一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...
- WinForm中DataGridView复制选中单元格内容解决方案
WinForm中DataGridView鼠标选中单元格内容复制方案 1.CTR+C快捷键复制 前提:该控件ClipboardCopyMode属性设置值非Disable: 2.鼠标框选,自定义代码实现复 ...
- jqgrid cellEdit为true的时候,默认选中单元格值的解决方案
jqgrid cellEdit为true的时候,点击单元格的时候,鼠标在单元格最前面闪. 这时候如果要修改数字内容,非常麻烦.要全选单元格内容,不然不好改. 点击单元格的时候,默认选中单元格值的解决方 ...
- GridView获取单个单元格的值
0.GridView中的所有数据都存储在Rows集合中,可以通过Rows的Cell属性获取单个单元格的值:如果某个单元格包含其他控件,则通过使用单元格的 Controls 集合,从单元格检索控件:如果 ...
随机推荐
- Css3-文字
一.text-overflow text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 语法:text-overflow:clip(默认属性,表示剪切) | ell ...
- ES6——函数-箭头函数
箭头函数: 1.普通函数 function 函数名(){...} 2.箭头函数 注意: 1)如果只有一个返回值,{}return可以省略: let arr = [12,5,8,99,33,14,26 ...
- es5和es6中的this指向问题
const test ={ id:2, a:function(){ var a_this=this; setTimeout(function(){ console.log('a:',this,a_th ...
- ViewMode
一.ViewMode 实现使用场景-Model枚举的情景下, 注意:枚举声明在后台的时候,需要渲染界面,页面表格使用 Bootstrap Table插件-事先通过ajax 渲染(数据库读取值1.2.3 ...
- qt如何去掉文件路径最后一个反斜杠的内容
QString strTemp = “E:\\hell\\word\\detect.txt” int nIndex = strTemp.lastIndexOf('\\'); strTemp ...
- java ArrayList存储基本类型
package java06; /* 如果希望像集合ArrayList中存储基本数据类型数据,必须使用基本数据类型对应的“包装类” 基本数据类型 包装类(引用类型,包装类都位于java.lang包下 ...
- 【串线篇】数据库设计之加谈n-n
// n-n即(1-n)+(n-1): // 1-n:n-1:n-n:外键应该放在哪个表? //结论: //一对n:外键一定放在n的一端:别让一个人去记14亿,而让大家只记一个习大大 //n-n:”中 ...
- 51nod 1605:棋盘问题
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1605 题目中最有用的点其实还是x必为奇数 #include& ...
- 英语单词forwarding
forwarding 来源——xshell的远程连接 [c:\~]$ Connecting to ... Connection established. To escape to local shel ...
- java通过url调用远程接口返回json数据
java通过url调用远程接口返回json数据,有用户名和密码验证, 转自 https://blog.csdn.net/wanglong1990421/article/details/78815856 ...