如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter
一、如何从 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的更多相关文章
- WPF备忘录(3)如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter
一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...
- 读取Excel文件中的单元格的内容和颜色
怎样读取Excel文件中的单元格的内容和颜色 先创建一个Excel文件,在A1和A2中随意输入内容,设置A1的字体颜色为红色,A2的背景为黄色.需要 using Excel = Microsoft.O ...
- 改变datagrid中指定单元格的值
//自己设置编辑时显示的内容 $('#purchasegroupname'+index).html(name); //单元格真实内容 $('#material_datagrid').datagrid( ...
- WPF:获取DataGrid控件单元格DataGridCell
转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...
- c# WPF DataGrid 获取选中单元格信息
private void Dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { Console.Write ...
- winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难
// winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...
- 获取wpf datagrid当前被编辑单元格的内容
原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...
- 如何把Excel中的单元格等对象保存成图片
对于Excel中的很多对象,比如单元格(Cell),图形(shape),图表(chart)等等,有时需要将它们保存成一张图片.就像截图一样. 最近做一个Excel相关的项目,项目中遇到一个很变态的需求 ...
- 【表格设置】HTML中合并单元格,对列组合应用样式,适应各浏览器的内容换行
1.常用表格标签 普通 <table> | <tr> | | <th ...
随机推荐
- audio_coding模块分析和audio_conference_mixer模块分析
audio_coding 1. 主要接口 AudioCodingModuleImpl::RegisterReceiveCodec 初始化Codec AudioCodingModul ...
- 0x31 质数
poj2689 算根号R的质数,然后把L~R区间(这个很小啊)的合数判下 #include<cstdio> #include<iostream> #include<cst ...
- CodeForces--606A --Magic Spheres(模拟水题)
Magic Spheres Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submi ...
- NEU2016年一月月赛回顾
月赛传送门 http://acm.neu.edu.cn/hustoj/contest.php?cid=1066 月赛已经结束十天了...才把题目补完真是大失误... 茅巨巨四天前就补完了,,,总结写得 ...
- python对象 -- 组合
详解组合:#Demo1class Game_kind: def __init__(self,nickname,sex,hp,ad): self.nickname = nickname self.sex ...
- [转自百度贴吧-本人亲测有效]Adobe XD 打开立即闪退问题修复
出现闪退的原因还是因为缺少C++组件, 下载 DirectXRepairV3.7软件 原文: https://tieba.baidu.com/p/5961511474 软件下载: http://xia ...
- 浅谈SpringCloud (三) Ribbon负载均衡
什么是负载均衡 当一台服务器的单位时间内的访问量越大时,服务器压力就越大,大到超过自身承受能力时,服务器就会崩溃.为了避免服务器崩溃,让用户有更好的体验,我们通过负载均衡的方式来分担服务器压力. 我们 ...
- js 判断设备的来源
function deviceType(){ var ua = navigator.userAgent; var agent = ["Android", "iPhone& ...
- win32应用禁止改变窗口大小方法
一种简单的处理方法是在调用CreateWindow函数时指定的窗口样式中去掉WS_THICKFRAME样式. 如果你使用的样式中已经包含该样式,例如WS_OVERLAPPEDWINDOW,我们可以將W ...
- Android自定义日历控件(继承系统控件实现)
Android自定义日历控件(继承系统控件实现) 主要步骤 编写布局 继承LinearLayout设置子控件 设置数据 继承TextView实现有圆圈背景的TextView 添加Attribute 添 ...