一、如何从 Datagrid 中获得单元格的内容

   DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items.

但是,WPF中的DataGrid 不同于Windows Forms中的 DataGridView。 在DataGrid的Items集合中,DataGridRow

是一个Item,但是,它里面的单元格却是被封装在 DataGridCellsPresenter 的容器中;因此,我们不能使用

DataGridView.Rows.Cells 这样的语句去获得单元格的内容。但是,在WPF中我们可以通过可视树(VisualTree)

去进入到控件“内部“, 那么,我们当然可以通过VisualTree进入DataGrid中的DataGridRow 和 DataGridCellsPresenter

并且得到在DataGridCellsPresenter中的实例, 大家可以通过以下的代码遍历VisualTree

DataGridRow rowContainer = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(rowIndex);
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); // ... public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T; if (child == null)
child = GetVisualChild<T>(v);
else
break;
} return child;
}

 二、WPF 使用值转换器进行绑定数据的转换IValueConverter

    有的时候,我们想让绑定的数据以其他的格式显示出来,或者转换成其他的类型,我们可以

使用值转换器来实现.比如我数据中保存了一个文件的路径”c:\abc\abc.exe”,但是我想让他在前台

列表中显示为”abc.exe”.首先我们先建一个IvalueConverter接口的类.

  

class GetFileName : IValueConverter
{
//Convert方法用来将数据转换成我们想要的显示的格式
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
FileInfo fi = new FileInfo((string)value);
return fi.Name;
}
//ConvertBack方法将显示值转换成原来的格式,因为我不需要反向转换,所以直接抛出个异常
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

为了使用这个转换器,我们要将项目的名称空间映射到xaml中,比如我项目名字为自动更新,用local作为空间名称前缀

xmlns:local="clr-namespace:命名空间"  

为了使用的更方便,我们在Resources集合中创建一个转换器对象

<Window.Resources>
<local:GetFileName x:Key="GetFileName"></local:GetFileName>
</Window.Resources>

现在我们去绑定数据的地方使用StaticResource来指向转换器

<TextBlock>
<TextBlock.Text>
<Binding Path="FileName">
<Binding.Converter>
<local:GetFileName></local:GetFileName>
</Binding.Converter>
</Binding>
</TextBlock.Text>
</TextBlock>

或者这样使用:

<TextBlock Text="{Binding Path=FileName,Converter={StaticResource GetFileName}}"  />

如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter的更多相关文章

  1. WPF备忘录(3)如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter

    一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...

  2. 读取Excel文件中的单元格的内容和颜色

    怎样读取Excel文件中的单元格的内容和颜色 先创建一个Excel文件,在A1和A2中随意输入内容,设置A1的字体颜色为红色,A2的背景为黄色.需要 using Excel = Microsoft.O ...

  3. 改变datagrid中指定单元格的值

    //自己设置编辑时显示的内容 $('#purchasegroupname'+index).html(name); //单元格真实内容 $('#material_datagrid').datagrid( ...

  4. WPF:获取DataGrid控件单元格DataGridCell

    转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...

  5. c# WPF DataGrid 获取选中单元格信息

    private void Dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { Console.Write ...

  6. winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难

    // winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...

  7. 获取wpf datagrid当前被编辑单元格的内容

    原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...

  8. 如何把Excel中的单元格等对象保存成图片

    对于Excel中的很多对象,比如单元格(Cell),图形(shape),图表(chart)等等,有时需要将它们保存成一张图片.就像截图一样. 最近做一个Excel相关的项目,项目中遇到一个很变态的需求 ...

  9. 【表格设置】HTML中合并单元格,对列组合应用样式,适应各浏览器的内容换行

    1.常用表格标签 普通    <table>           |           <tr>          |           |          <th ...

随机推荐

  1. js面向对象编程:怎样实现方法重载

    js中怎样实现方法重载?这涉及到三个问题 1同名函数的调用问题 2函数中特殊的參数arguments 3怎样利用arguments实现方法重载 1同名函数的调用问题 都知道在js中假设存在多个名称同样 ...

  2. Sqoop_具体总结 使用Sqoop将HDFS/Hive/HBase与MySQL/Oracle中的数据相互导入、导出

    一.使用Sqoop将MySQL中的数据导入到HDFS/Hive/HBase watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWFyb25oYWRvb3A=/ ...

  3. Javascript中的with用法

    1.看例子 <script language="javascript"> function Lakers() { this.name = "kobe brya ...

  4. 蓝桥杯-- 历届试题 核桃的数量 (gcd)

      历届试题 核桃的数量   时间限制:1.0s   内存限制:256.0MB        问题描述 小张是软件项目经理,他带领3个开发组.工期紧,今天都在加班呢.为鼓舞士气,小张打算给每个组发一袋 ...

  5. [JavaEE]Spring配置文件总结

    首先来看一个标准的Spring配置文件 applicationContext.xml <?xml version="1.0" encoding="UTF-8&quo ...

  6. java多线程 interrupt(), interrupted(), isInterrupted()方法区别

    interrupt()方法: 作用是中断线程. 本线程中断自身是被允许的,且"中断标记"设置为true 其它线程调用本线程的interrupt()方法时,会通过checkAcces ...

  7. Spring《五》集合的注入方式

    List.Set.Map.Properties 1.List <property name="msg"> <list> <value>gf< ...

  8. 使用jquery获取ul中当前正在点击的li的索引

    <ul class="list"> <li>哈哈</li> <li>呵呵</li> <li>嘻嘻</l ...

  9. log4j:WARN Please initialize the log4j system properly.解决方案

    在使用quarz任务调度框架时的错误,实际上这个问题很常见,并不影响程序的使用,只是缺少日志输出,完整错误信息: log4j:WARN No appenders could be found for ...

  10. ajax请求及遍历

    $(".btn").click(function(){ $.ajax({ type:"POST", url:"../../js/info.json&q ...