我希望在我的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>
我想知道是否是因为父级1级别实际上是我的LineCoords,所以我是否必须再次上级?干杯。
答案 0 :(得分:1)
LDLTrackViewModel
不是有效的AncestorType
,因为它不是可视树中的元素。
您应绑定到父ContentPresenter
的父ContentPresenter
:
Command="{Binding DataContext.FooCommand, RelativeSource={RelativeSource AncestorType=ContentPresenter, AncestorLevel=2}}" />