Wpf 正在尝试绑定到my ItemsControl中的父视图模型绑定

Wpf 正在尝试绑定到my ItemsControl中的父视图模型绑定,wpf,mvvm,binding,parent,Wpf,Mvvm,Binding,Parent,我希望绑定到ItemsControl代码中的视图模型。但是,我的相对源绑定似乎没有正确绑定 <ItemsControl ItemsSource="{Binding LDLTracks}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Canvas/>

我希望绑定到ItemsControl代码中的视图模型。但是,我的相对源绑定似乎没有正确绑定

<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,所以我必须再次升级一级吗?干杯。

LDLTrackViewModel
不是有效的
存储类型,因为它不是可视化树中的元素

您应该绑定到父级
ContentPresenter
的父级
ContentPresenter

Command="{Binding DataContext.FooCommand, RelativeSource={RelativeSource AncestorType=ContentPresenter, AncestorLevel=2}}" />

很好用,谢谢。我想我需要离开这里,学习更多关于绑定的知识,因为我永远不会明白这一点。