Binding中使用RelativeSource
问题:当前View有一个DataGrid,其DataGridTemplateColum设置了一个ComboBox。
其对应的VM是local:ViewMode,含有ModelValue和myLIst属性分别用来做DataGrid的ItemSource,和ComboBox的ItemSource。外层内层看起来链接的VM层不同的property。
初始代码如下:

<DataGrid ItemsSource="{Binding ModelValues, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<DataGridTemplateColumn Header="myHeader" Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox DisplayMemberPath="Value" SelectedValuePath="Key" IsEnabled="False"
SelectedValue="{Binding myID, Mode=TwoWay}"
ItemsSource="{Binding Path=myList,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ViewModel}}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox DisplayMemberPath="Value" SelectedValuePath="Key" IsEnabled="False"
SelectedValue="{Binding myID, Mode=TwoWay}"
ItemsSource="{Binding Path=myList,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ViewModel}}}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid>

错误在于RelativeSource沿着Visual Tree找不到一个叫local:ViewModel的图形对象
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ViewModel}}
解决代码:The Relativesource is for the Visual Tree, not the DataContext

<DataGrid ItemsSource="{Binding ModelValues, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<DataGridTemplateColumn Header="myHeader" Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox DisplayMemberPath="Value" SelectedValuePath="Key" IsEnabled="False"
SelectedValue="{Binding myID, Mode=TwoWay}"
ItemsSource="{Binding Path=DataContext.myList,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox DisplayMemberPath="Value" SelectedValuePath="Key" IsEnabled="False"
SelectedValue="{Binding myID, Mode=TwoWay}"
ItemsSource="{Binding Path=DataContext.myList,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid>
Binding中使用RelativeSource的更多相关文章
- Binding 中 Elementname,Source,RelativeSource 三种绑定的方式
在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到过的问题做下汇总记录和理解. 1. so ...
- WPF的控件Binding的ElementName/RelativeSource具体用法
<TextBlock Name="_txtSickBedNo" FontStyle="Normal" Foreground="Black&quo ...
- 【转】WPF中Binding的技巧(一)
WPF中Binding的技巧(一) 在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到 ...
- [转]WPF中Binding的技巧
在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到过的问题做下汇总记录和理解. 1. so ...
- 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换
[源码下载] 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数 ...
- 【转】WPF中的Binding技巧(二)
WPF中的Binding技巧(二) 接上篇, 我们来看一看Elementname,Source,RelativeSource 三种绑定的方式 1.ElementName顾名思义就是根据Ui元素 ...
- 在WPF中如何使用RelativeSource绑定
在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource. 这种办法的意思是指当前元素和绑定源的位置关系. 第一种关系: Self 举一个最简单的例子:在一个StackPanel ...
- 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧
[源码下载] 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧 作者:webabcd 介绍背水一战 Wind ...
- 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定
[源码下载] 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定 作者:we ...
随机推荐
- Lazy Scheduler:我的轻量级任务调度框架
一.背景 工作中经常涉及任务调度,一直都是采用while(true) => if hitted DO => Thread.Sleep(interval)的模式.但是最近实在是感觉这种实现模 ...
- 给 DiscuzX3 缩略图添加水印
Discuz X3 默认开启缩略图的时候水印只添加到原图上面,而缩略图上面无法进行水印图的添加,需要改下程序,方可给缩略图添加水印,需要修改2个地方: 1.打开 source\function\fun ...
- Vue的常用指令v-if, v-for, v-show,v-else, v-bind, v-on
Vue.js的指令是以v-开头的,它们作用于HTML元素,指令提供了一些特殊的特性,将指令绑定在元素上时,指令会为绑定的目标元素添加一些特殊的行为,我们可以将指令看作特殊的HTML特性(attribu ...
- 1019(C++)
计算n个数的最小公倍数,可用欧几里得算法计算两个数字的最大公约数,再计算两个数最小公倍数 有了2个数最小公倍数算法就简单了,即为:计算第一和第二个数得到最小公倍数lc,再计算lc和第三个数最小公倍数. ...
- 关于可变参数列表stdarg
1.对于可变参数 可变参数列表通过stdarg.h中的宏实现: 主要有一个va_list类型 三个宏:va_start,va_arg,va_end; va_start格式: va_start(argu ...
- 拉格朗日对偶与kkt条件
- java 实体序列化的意义
一.序列化的意义 客户端访问了某个能开启会话功能的资源, web服务器就会创建一个与该客户端对应的HttpSession对象,每个HttpSession对象都要站用一定的内存空间.如果在某一时间段内访 ...
- Win7 Visual Studio 2008如何注册
默认是90天试用 在控制面板中卸载程序,然后找到VS2008,点击卸载/更改 到这一步就不要动了 去下载并运行CrackVS2008ForWindows7,然后点击右上角的Bug微软,弹出 ...
- Unity里面的自动寻路(一)
来自:http://www.narkii.com/club/forum.php?mod=viewthread&tid=269146&highlight=Unity%E9%87%8C%E ...
- 《Android源代码设计模式解析与实战》读书笔记
1.定义 将对象组合成树形结构以表示"部分-总体"的层次结构,使得用户对单个对象和组合对象的使用具有一致性. 2.使用场景 (1)表示对象的部分-总体层次结构时. (2)从一个总体 ...