我希望绑定到WPF列表中的列表

问题描述 投票:1回答:1

我已将ItemsSource中的ItemsControl绑定到ViewModel列表,称为LDLTracks。在LDLTrack视图模型中,有一个我希望绑定的坐标对象列表,但是我不确定正确的方法。

我可以通过将我的画布绑定到TrackViewModel列表然后在我的

XAML:

<ItemsControl ItemsSource="{Binding LDLTracks}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Canvas/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>

                    <ItemsControl.ItemTemplate>
                        <DataTemplate>

                            <Line  X1="{Binding X1}" Y1="{Binding Y1}" X2="{Binding X2}" Y2="{Binding Y2}" Stroke="{Binding LineColor}" StrokeThickness="5">
                                <Line.InputBindings>
                                    <MouseBinding Gesture="LeftClick" Command="{Binding FooCommand}"/>
                                </Line.InputBindings>
                            </Line>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

我希望将{Binding X1}替换为坐标列表,理想情况下它将是Coordinates.X1,因为坐标将是一个列表,但是当我尝试时,您可以绑定的唯一属性是坐标列表计数。有任何想法吗?

c# wpf mvvm binding viewmodel
1个回答
2
投票

你可以使用绑定到ItemsControl列表的内部/嵌套Coordinates

<ItemsControl ItemsSource="{Binding LDLTracks}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding Coordinates}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Line X1="{Binding X1}" Y1="{Binding Y1}" X2="{Binding X2}" Y2="{Binding Y2}"
                              Stroke="{Binding LineColor}" StrokeThickness="5">
                            <Line.InputBindings>
                                <MouseBinding Gesture="LeftClick" Command="{Binding FooCommand}"/>
                            </Line.InputBindings>
                        </Line>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
© www.soinside.com 2019 - 2024. All rights reserved.