2018-8-10-wpf-绑定-DataGridTextColumn-
title | author | date | CreateTime | categories |
---|---|---|---|---|
wpf 绑定 DataGridTextColumn
|
lindexi
|
2018-08-10 19:16:53 +0800
|
2018-2-13 17:23:3 +0800
|
WPF
|
本文告诉大家如何在 DataGridTextColumn 使用绑定,因为很容易绑定就找不到数据。
使用 DataGrid 有一个坑,就是 Columns 的数据绑定拿不到数据。
例如下面的代码
<Grid>
<DataGrid x:Name="MyDataGrid" ItemsSource="{Binding DataList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
<DataGridTextColumn Header="Property1" Binding="{Binding Property1}"/>
<DataGridTextColumn Header="Property2" Binding="{Binding Property2}"/> <DataGridTemplateColumn Header="Total">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding>
<Binding Path="Property1"/>
<Binding Path="Property2"/>
<MultiBinding.Converter>
<local:MyValueConverter/>
</MultiBinding.Converter>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
可以看到绑定了DataGridTextColumn Header="Property1" Binding="{Binding Property1}"
可以拿到数值,但是在MultiBinding
没有拿到数值,因为他在DataTemplate
而DataGridTemplateColumn
没有数据。
解决方法很简单,使用RelativeSource
找到数据。只需要修改<Binding Path="DataContext.Property2" RelativeSource="{RelativeSource AncestorType=DataGridCell}"/>
就可以。下面就是修改后的代码
<Grid>
<DataGrid x:Name="MyDataGrid" ItemsSource="{Binding DataList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
<DataGridTextColumn Header="Property1" Binding="{Binding Property1}"/>
<DataGridTextColumn Header="Property2" Binding="{Binding Property2}"/>
<DataGridTextColumn>
<DataGridTextColumn.Binding >
<MultiBinding >
<Binding Path="DataContext.Property1" RelativeSource="{RelativeSource AncestorType=DataGridCell}"/>
<Binding Path="DataContext.Property2" RelativeSource="{RelativeSource AncestorType=DataGridCell}"/>
<MultiBinding.Converter>
<local:MyValueConverter/>
</MultiBinding.Converter>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
2018-8-10-wpf-绑定-DataGridTextColumn-的更多相关文章
- 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧
[源码下载] 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧 作者:webabcd 介绍背水一战 Wind ...
- WPF快速入门系列(4)——深入解析WPF绑定
一.引言 WPF绑定使得原本需要多行代码实现的功能,现在只需要简单的XAML代码就可以完成之前多行后台代码实现的功能.WPF绑定可以理解为一种关系,该关系告诉WPF从一个源对象提取一些信息,并将这些信 ...
- WPF绑定のRelativeSource
在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource. 这种办法的意思是指当前元素和绑定源的位置关系. 第一种关系: Self 举一个最简单的例子:在一个StackPanel ...
- WPF DataGrid DataGridTextColumn
Style设置时,无法绑定到数据,需要这样写 1 <DataGridTextColumn Header="呵呵" Binding="{Binding ID}&quo ...
- 【转】WPF 给DataGridTextColumn统一加上ToolTip
源地址:http://dongguojun.iteye.com/blog/1671963 我发现WPF中DataGridTextColumn直接设置它的ToolTipService.Tooltip并不 ...
- 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合
[源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...
- 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换
[源码下载] 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换 作者:webabcd 介 ...
- 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定
[源码下载] 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定 作者:we ...
- 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue
[源码下载] 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue 作者:weba ...
- 申请Office 365一年免费的开发者账号攻略(2018年10月份版本)
要进行Office 365开发,当然需要有完整的Office 365环境才可以.为了便于广大开发人员快速地启动这项工作,微软官方给所有开发人员提供了免费的一年开发者账号 那么如何申请Office ...
随机推荐
- ForkJoin简单示例
import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import jav ...
- CF883J 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest - J. Renovation 贪心+树状数组
首先对于一个月的预算,如果非常小的话,我们可以留到后面的 \(a_i\) 最大的月来用,因为 \(a_i\) 越大能够拆建筑的越多. 于是我们把 \(a_i\) 合并给 \(i\) 后面的 \(a\) ...
- vue+element-ui 实现分页(根据el-table内容变换的分页)
官方例子 官方提示: 设置layout,表示需要显示的内容,用逗号分隔,布局元素会依次显示.prev表示上一页,next为下一页,pager表示页码列表,除此以外还提供了jumper和total,si ...
- 日常Git使用——2019年12月11日16:19:03
1.git介绍 1.1 什么是git? 什么是Git? 比如一个项目,两个人同时参与开发,那么就把这个项目放在一个公共的地方,需要的时候都可以去获取,有什么改动,都可以进行提交. 为了做到这一点,就需 ...
- JAVA学习笔记--方法中的参数调用是引用调用or值调用
文献来源:<JAVA核心技术卷Ⅰ>,第4章第5节 (没有相关书籍的可看传送门) ps:测试demo因为偷懒,用的是String对象 结论:Java使用的是对象的值引用.即将任何对象所在内存 ...
- Shiro安全框架的说明及配置入门
Shiro是什么? Shiro是一个非常强大的,易于使用的,开源的,权限框架.它包括了权限校验,权限授予,会话管理,安全加密等组件 什么时候使用它呢? 如果你是设计RBAC基础系统,需要编写大量用于权 ...
- centos6.8下安装nginx
我用的阿里云上的镜像,make的时候总是出错,后来说是gcc安装不完整,要重新用下面命令安装 下: yum install gcc gcc-c++ gcc-g77 接下来下载nginx用到的一些库 w ...
- 【HDU6609】Find the answer【线段树】
题目大意:给你一个序列,对于每个i,你可以选择1~i-1中任意多的数并将它删去,剩余的数(包括i)∑≤m,问对于每个i最少删几个数可以达到要求 题解: 考虑朴素的思想,对于每个i,我只需要删去最大的若 ...
- 从Java future 到 Guava ListenableFuture实现异步调用
从Java future 到 Guava ListenableFuture实现异步调用 置顶 2016年04月24日 09:11:14 皮斯特劳沃 阅读数:17570 标签: java异步调用线程非阻 ...
- src,href 和rel的区别
src用于替换当前元素,值一般是引用的文件的绝对路径或者相对路径 href 属性的值可以是任何有效文档的相对或绝对URL,包括片段标识符和JavaScript代码段 rel REL属性用于定义链接的文 ...