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>

私の例では、テンプレートに同じタイプのオブジェクトが 2 つありますが、そこには他のコントロール タイプもいくつかあります。

4

1 に答える 1

2

内に複数の要素がありますDataTemplate。2 つのPathオブジェクトをPanel、たとえば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などに別の値を使用することをお勧めします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 に答える