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没有拿到数值,因为他在DataTemplateDataGridTemplateColumn没有数据。

解决方法很简单,使用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-的更多相关文章

  1. 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧

    [源码下载] 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧 作者:webabcd 介绍背水一战 Wind ...

  2. WPF快速入门系列(4)——深入解析WPF绑定

    一.引言 WPF绑定使得原本需要多行代码实现的功能,现在只需要简单的XAML代码就可以完成之前多行后台代码实现的功能.WPF绑定可以理解为一种关系,该关系告诉WPF从一个源对象提取一些信息,并将这些信 ...

  3. WPF绑定のRelativeSource

    在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource. 这种办法的意思是指当前元素和绑定源的位置关系. 第一种关系: Self 举一个最简单的例子:在一个StackPanel ...

  4. WPF DataGrid DataGridTextColumn

    Style设置时,无法绑定到数据,需要这样写 1 <DataGridTextColumn Header="呵呵" Binding="{Binding ID}&quo ...

  5. 【转】WPF 给DataGridTextColumn统一加上ToolTip

    源地址:http://dongguojun.iteye.com/blog/1671963 我发现WPF中DataGridTextColumn直接设置它的ToolTipService.Tooltip并不 ...

  6. 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合

    [源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...

  7. 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换

    [源码下载] 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换 作者:webabcd 介 ...

  8. 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定

    [源码下载] 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定 作者:we ...

  9. 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue

    [源码下载] 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue 作者:weba ...

  10. 申请Office 365一年免费的开发者账号攻略(2018年10月份版本)

    要进行Office 365开发,当然需要有完整的Office 365环境才可以.为了便于广大开发人员快速地启动这项工作,微软官方给所有开发人员提供了免费的一年开发者账号   那么如何申请Office ...

随机推荐

  1. (ACM模板)二元组pair

    #include<iostream> #include<cstdio> #include<utility> using namespace std; typedef ...

  2. 【LeetCode】随机化算法 random(共6题)

    [384]Shuffle an Array(2019年3月12日) Shuffle a set of numbers without duplicates. 实现一个类,里面有两个 api,struc ...

  3. Sass:字符串函数-To-upper-case()、To-lower-case()

    1.To-upper-case() To-upper-case() 函数将字符串小写字母转换成大写字母.如: //SCSS .test { text: to-upper-case(aaaaa); te ...

  4. mysql跟踪sql

    mysql中执行的sql跟踪比oracle简单多了,它自身有log.我们只要打开这个log记录,执行sql语句,再查看这个log就可以了.1. 首先要打开log的开关使用命令:show variabl ...

  5. ORACLE 行转列的通用过程

    --测试数据create table rowtocol_test asselect 2009 year,1 month,'部门1' dept,50000 expenditure from dualun ...

  6. 【leetcode】828. Unique Letter String

    题目如下: A character is unique in string S if it occurs exactly once in it. For example, in string S = ...

  7. LDD3 第7章 Time,Delays and Deferred Work

    处理时间委托包括如下任务,按复杂度依次上升: 测量时间流失和比较时间 知道当前时间 指定时间量的延时操作 调度异步函数在之后的时间发生 一.测量时间流失 系统定时硬件规律的产生定时器中断,在内核启动阶 ...

  8. 2018-2019 2 20165203 《网络对抗技术》Exp8 Web基础

    2018-2019 2 20165203 <网络对抗技术>Exp8 Web基础 实验要求 1.本实践的具体要求有: (1) Web前端HTML(0.5分) 能正常安装.启停Apache.理 ...

  9. html中map标签和area标签的应用(总结)

    html中map标签和area标签的应用(总结) 一.总结 一句话总结: html中map标签和area标签和组成图片地图,在前端优化中可以减少http请求 1.map标签的用途是什么? 图片地图:是 ...

  10. ubuntu 配置jre后出现问题Error occurred during initialization of VM

    百度了好久,找到了一个可以解决的办法. https://blog.51cto.com/chris2013/1313117 就是在usr/java/jre/lib/rt.pack需要解压成rt.jar ...