wpf - 尝试绑定到我的 ItemsControl 中的父视图模型绑定
问题描述
我希望在我的 ItemsControl 代码中绑定到我的 LDLTracks View 模型。但是,我的相对源绑定似乎没有正确绑定。
<ItemsControl ItemsSource="{Binding LDLTracks}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding LineCoords}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Line X1="{Binding X1}" Y1="{Binding Y1}" X2="{Binding X2}" Y2="{Binding Y2}" Stroke="Black" StrokeThickness="5">
<Line.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type viewModel:LDLTrackViewModel}}, Path=FooCommand}"/>
</Line.InputBindings>
</Line>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我想知道是不是因为上一级的父级实际上是我的 LineCoords,所以我必须再上一级吗?干杯。
解决方案
LDLTrackViewModel
不是有效AncestorType
的,因为它不是可视树中的元素。
您应该绑定到 parentContentPresenter
的 parent ContentPresenter
:
Command="{Binding DataContext.FooCommand, RelativeSource={RelativeSource AncestorType=ContentPresenter, AncestorLevel=2}}" />
推荐阅读
- statistics - 比较平滑方法最合适的方法是什么?
- python - 有没有一种简单的方法可以找到声音信号中包络的频率?
- django - Django 表单仅在我按下提交按钮时显示
- authentication - Auth0 - 使用动态回调的 OAuth 登录
- forms - 发布请求后找不到 Flask 页面
- amazon-dynamodb - 按字符串数组搜索 DynamoDB 字符串集属性
- function - 如何在 foreach 并行循环中调用函数
- terraform - ECS集群基于CPU/内存的扩缩容任务
- certbot - Certbot 忽略 webroot 文件夹
- javascript - 在 ReactJS 中从 AD 合作伙伴加载外部脚本