WPF备忘录(3)如何从 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 = ; 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}}" />
WPF备忘录(3)如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter的更多相关文章
- 如何从 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 /* ------------------------------------------- ...
- 获取wpf datagrid当前被编辑单元格的内容
原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...
- c# WPF DataGrid 获取选中单元格信息
private void Dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { Console.Write ...
- 潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据
原文:潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据 目前自己对treeview的感慨很多 今天先讲 面对这种 表结构的数据 的其中 ...
- winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难
// winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...
- 如何把Excel中的单元格等对象保存成图片
对于Excel中的很多对象,比如单元格(Cell),图形(shape),图表(chart)等等,有时需要将它们保存成一张图片.就像截图一样. 最近做一个Excel相关的项目,项目中遇到一个很变态的需求 ...
随机推荐
- 数据库中表的位置,在sysdatabases中
name dbid sid mode status status2 crdate reserved category cmptlevel filename version master :: :: C ...
- 等到花儿也谢了的await
async/task/await三组合是.NET Framework 4.5带给.NET开发者的大礼,合理地使用它,可以提高应用程序的吞吐能力. 但是它的使用有点绕人,如果不正确使用,会带来意想不到的 ...
- SignalR简介
什么是SignalR? ASP.NET SignalR是ASP.NET开发人员的库,它简化了向应用程序添加实时Web功能的过程.实时Web功能是指服务器代码在连接的客户端可用时立即将内容推送到连接的客 ...
- ScintillaNET的应用
出于工作需要,需要制作一个嵌入在桌面应用中的C语言编辑器,经过一系列调研,目前ScintillaNET应该是最合适的了,开源.轻便.功能丰富,但是踩得坑也很多,接下面一一说道. 目前Scintilla ...
- 背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar
[源码下载] 背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) App ...
- ceph 源码安装 configure: error: "Can't find boost spirit headers"
问题:configure: error: "Can't find boost spirit headers" 解决: 推荐:sudo apt-get install libboos ...
- Python 爬虫(二十五) Cookie的处理--cookielib库的使用
Python中cookielib库(python3中为http.cookiejar)为存储和管理cookie提供客户端支持. 该模块主要功能是提供可存储cookie的对象.使用此模块捕获cookie并 ...
- 预处理命令使用详解----#if、#endif、#undef、#ifdef、#else、#elif
预处理命令 在接触#if.#undef这类预处理指令前,大部分都都接触过#define.#include等预处理命令,通俗来讲预处理命令的作用就是在编译和链接之前,对源文件进行一些文本方面的操作,比如 ...
- 写vue项目时候 零星的笔记
1,挂载也可以用 .$mount() 2,子组件中通过this.$root拿到实例的数据.截图中是子组件中
- C#6.0语言规范(十三) 接口
接口定义合同.实现接口的类或结构必须遵守其合同.接口可以从多个基接口继承,并且类或结构可以实现多个接口. 接口可以包含方法,属性,事件和索引器.接口本身不为它定义的成员提供实现.接口仅指定必须由实现接 ...