1

在下面的 XAML 中,请填写“WhatGoesHere”节点并向我解释如何不会弄乱Canvas.

<ItemsControl ItemsSource="{Binding ViewModels}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <WhatGoesHere?>
                <Path Stroke="CornflowerBlue" StrokeThickness="2">
                    <Path.Data>
                        <PathGeometry Figures="{Binding Figures}"/>
                    </Path.Data>
                </Path>
                <Path Stroke="Red" StrokeThickness="2">
                    <Path.Data>
                        <PathGeometry Figures="{Binding Figures2}"/>
                    </Path.Data>
                </Path>
            </WhatGoesHere?>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我的示例在模板中有两个相同类型的对象,但我也会在其中有几个其他控件类型。

4

1 回答 1

2

您在 a 中有多个元素DataTemplate。您必须将您的两个Path对象放入某种 中Panel,例如 a Grid。问题是你所有的画布坐标在哪里计算,以便你可以绑定Grid到它?在您的视图模型中?然后你可以绑定到它,它看起来有点像这样:

<ItemsControl ItemsSource="{Binding ViewModels}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Canvas.Top="{Binding Y}" Canvas.Left="{Binding X}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <Path Stroke="CornflowerBlue" StrokeThickness="2">
                    <Path.Data>
                        <PathGeometry Figures="{Binding Figures}"/>
                    </Path.Data>
                </Path>
                <Path Stroke="Red" StrokeThickness="2" Grid.Row="1">
                    <Path.Data>
                        <PathGeometry Figures="{Binding Figures2}"/>
                    </Path.Data>
                </Path>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

如果您没有这些坐标,那么我建议您使用另一个值,ItemsPanelTemplate例如 a VirtualizedStackPanel。这可能看起来像这样:

<ItemsControl ItemsSource="{Binding ViewModels}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizedStackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Path Stroke="CornflowerBlue" StrokeThickness="2">
                    <Path.Data>
                        <PathGeometry Figures="{Binding Figures}"/>
                    </Path.Data>
                </Path>
                <Path Stroke="Red" StrokeThickness="2" Grid.Row="1">
                    <Path.Data>
                        <PathGeometry Figures="{Binding Figures2}"/>
                    </Path.Data>
                </Path>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

如果你能告诉我你真正想要达到的目标,我相信我能以更好的方式帮助你。

于 2013-04-27T12:05:18.607 回答