Binding中使用RelativeSource
问题:当前View有一个DataGrid,其DataGridTemplateColum设置了一个ComboBox。
其对应的VM是local:ViewMode,含有ModelValue和myLIst属性分别用来做DataGrid的ItemSource,和ComboBox的ItemSource。外层内层看起来链接的VM层不同的property。
初始代码如下:
![](/assets/blank.gif)
![](/assets/blank.gif)
<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>View Code
![](/assets/blank.gif)
错误在于RelativeSource沿着Visual Tree找不到一个叫local:ViewModel的图形对象
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ViewModel}}
解决代码:The Relativesource is for the Visual Tree, not the DataContext
![](/assets/blank.gif)
![](/assets/blank.gif)
<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>
转载于:https://www.cnblogs.com/sjqq/p/8110358.html
Binding中使用RelativeSource相关推荐
- 关于 WCF Binding 中 OpenTimeout, CloseTimeout, SendTimeout, ReceiveTimeout 的解释
关于这四个属性,在MSDN中的解释有点敷衍了事. Open/Close/Receive/Send本是HTTP/TCP/SOCKET的概念,Read/Write Operation则是Web Servi ...
- C#WPF Binding 中的Path什么意思
<TextBox x:Name="txt1" /> <TextBlock x:Name="txt2" Text="{Binding ...
- 在WP7下自定义RelativeSource 的Binding
最近老没有时间上来写博文.今天有空来写写上次还没有说完的话题.上一篇提到说说我在WP7应用开发中遇到的 子控件 DataTemplete 中的按钮的命令绑定,刚开始接触似乎是个头疼的问题.那怎么解决呢 ...
- 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换...
重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换 原文: ...
- 为什么WPF在code behind使用RelativeSource的binding会出现错误
不知道是不是有小伙伴遇到过这样的情况.当我们尝试将一个RelativeSource从xaml转到code behind时,原本好好的binding表达式居然出现binding错误. 我们来做个实验,我 ...
- 在WPF中如何使用RelativeSource绑定
在WPF中如何使用RelativeSource绑定 原文: 在WPF中如何使用RelativeSource绑定 在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource. 这种办 ...
- 【Groovy】Groovy 脚本调用 ( Groovy 类中调用 Groovy 脚本 | 参考 Script#evaluate 方法 | 创建 Binding 对象并设置 args 参数 )
文章目录 一.Groovy 类中调用 Groovy 脚本 1.参考 Script#evaluate 方法分析 Groovy 类中调用 Groovy 脚本 2.创建 Binding 对象并设置 args ...
- 【Groovy】Groovy 脚本调用 ( Groovy 脚本中调用另外一个 Groovy 脚本 | 绑定作用域 binding 变量分析 | Binding 类 variables 成员分析 )
文章目录 一.绑定作用域 binding 变量分析 二.Binding 类 variables 成员分析 三.完整代码示例 1.调用者 Groovy 脚本 2.被调用者 Groovy 脚本 3.执行结 ...
- 详解Flutter中各种Binding
前言 Flutter中所有的运转都是在各种Binding中调度的,也正是这些绑定器的存在彻底解耦了Widget . Element .RenderObject 对 Platform端的依赖,阅读此文需 ...
最新文章
- 行情分析:下杀或不可持续,市场大概率继续震荡
- 【Vegas原创】exp时,ORA-00932: 数据类型不一致解决方法
- 服务器500_BTA齐聚新基建——2030年百度智能云服务器数量超500万台
- Silverlight OA源代码(Silverlight4+SQLServer2005)
- dataframe groupby_PySpark SQL——SQL和pd.DataFrame的结合体
- python导入openpyxl报错问题,终于解决啦
- 20172310 2017-2018-2 《程序设计与数据结构》第十一周学习总结
- 探索ArrayList自动改变size真相
- mysql开启慢查询方法(转)
- Gym - 101981I-MagicPotion-最大流
- 前端学习(5):深入了解网站开发
- GitHub一份深度学习资源,包括论文、数据集、课程、图书、博客、教程、框架
- Enterprise Manager 无法连接到数据库实例。下面列出了组件的状态。
- 代码整洁之道读书笔记----第三章---函数--第一节-专注且短小
- [公告]博客迁移通知
- 用于预测的神经网络模型,神经网络模型可解释性
- C语言程序入门(零基础入门)
- 手把手教你使用YOLOV5训练自己的目标检测模型-口罩检测-视频教程
- 微服务的技术架构路线
- 如何用Matlab做一个3D头像
热门文章
- 航空机票APP的设计与实现【AS开发+SQLITE】【附源码】
- Ubuntu 升级到 20.04.2 后启动系统电脑黑屏,左上角光标闪烁
- oracle 单引号 逗号,Oracle中的单引号(转义)
- node_addon_api 实现c++addons helloworld详解
- 无法打开mysql因为apple_无法打开“XXXX”,因为Apple无法检查其是否包含恶意软件。怎么解决?...
- react放大镜组件
- Win7蓝屏APC_INDEX_MISMATCH解决方案(声卡)
- CentOS_7的换源+httpd及启动方法
- VMWare修改MAC地址
- php仿照微信头像截取,手把手教你用 PHP 拼接微信好友头像