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 集合,从单元格检索控件:如果 ...
随机推荐
- simple_pt时遇到的问题
elf.c:30:18: fatal error: gelf.h: No such file or directory 安装libelf-dev 遇到找不到ldwarf apt-cache sear ...
- 北京太速科技有限公司 layout 事业部
涵盖领域设计能力 ·通信板卡:PCI/PCIE/CPCI/VPX/光通信/无线通信/射频雷达/显卡 1.最小线宽:2MIL·主板服务器:电脑主板/交换机/服 ...
- 微信小程序(17)-- RSA加密 解密 加签 验签
RSA加密 解密 加签 验签 /** * 注:区分RSA私钥的类型,有pkcs1和pkcs8.pkcs8格式的私钥主要用于Java中 pkcs1格式: -----BEGIN RSA PRIVATE K ...
- Docker的使用(未完待续)
一.帮助命令 docker version docker info docker --help 二.镜像命令 列出机器上所有的镜像 docker images 查找某个镜像 docker search ...
- PHP: 双层 for循环的执行过程
先看代码 <?php for ($i = 0; $i<3; $i++) { for ($x = 0; $x<3; $x++) { // code... } } ?> 看下它的执 ...
- Python lambda 多变量
>> x = list(map(lambda x, y: (x>3, y>0), (1,2,3,4,5), (0,1,2,3,4)))>>> x[(False ...
- springmvc中拦截器配置格式
对于springmvc,有两种方式配置拦截器. 一是实现HandlerInterceptor接口,如 public class MyInterceptor1 implements HandlerInt ...
- Ubuntu12.04安装配置vncserver
安装 sudo apt-get install vnc4server 修改配置文件 sudo vim ~/.vnc/xstartup #!/bin/sh # Uncomment the followi ...
- 企业级Web服务器安全主动防御措施
篇一 : 企业级Web服务器安全主动防御措施 Web服务器现在已经成为了病毒.木马的重灾区.不但企业的门户网站被篡改.资料被窃取,而且还成为了病毒与木马的传播者.有些Web管理员采取了一些措施,虽然可 ...
- JS中数据结构之字典
字典是一种以键 - 值对形式存储数据的数据结构 通过数组实现字典 function Dictionary() { this.add = add; this.datastore = new Array( ...