WPF / C#:用线条动态连接形状

时间:2015-07-09 06:47:41

标签: c# .net wpf canvas

我有以下XAML代码:

    <Grid>
    <ItemsControl ItemsSource="{Binding Items}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas></Canvas>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Ellipse Width="{Binding Radius}" Height="{Binding Radius}" Fill="{Binding BackColor}" />
                    <Line Stroke="Black" StrokeThickness="1" x:Name="_Line"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Canvas.Left" Value="{Binding X}"/>
                <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                <Setter Property="Line.X1" Value="{Binding LineX1}"/>
                <Setter Property="Line.X2" Value="{Binding LineX2}"/>
                <Setter Property="Line.Y1" Value="{Binding LineY1}"/>
                <Setter Property="Line.Y2" Value="{Binding LineY2}"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
</Grid>

这是一个ItemsControl,其ItemPanel是一个Canvas,用于动态地将椭圆从我绑定的Obervable Collection中放置。使用Canvas.Top和Canvas.Left值的椭圆的放置工作正常。但它没有绘制连接椭圆的线条。

在后面的代码中正确计算了行的位置,我检查了它。

1 个答案:

答案 0 :(得分:1)

不应将“线”属性应用于“项容器”。相反,它们应该直接在DataTemplate中的Line控件上设置:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Ellipse Width="{Binding Radius}" Height="{Binding Radius}"
                     Fill="{Binding BackColor}" />
            <Line Stroke="Black" StrokeThickness="1" x:Name="_Line"
                  X1="{Binding LineX1}" Y1="{Binding LineY1}"
                  X2="{Binding LineX2}" Y1="{Binding LineY2}"/>
        </Grid>
    </DataTemplate>
</ItemsControl.ItemTemplate>

请注意,LineX和LineY属性值是相对于项目位置的。

编辑:绝对放置所有元素的替代解决方案不是在项容器上设置Canvas.Left和Canvas.Top属性(即根本没有ItemContainerStyle)。

您将使用带有EllipseGeometry而不是Ellipse的Path,并将EllipseGeometry的Center属性绑定到类型Point的视图模型属性,以替换X和Y:

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Canvas>
                <Path Fill="{Binding Fill}" Panel.ZIndex="1">
                    <Path.Data>
                        <EllipseGeometry Center="{Binding Center}"
                                         RadiusX="{Binding Radius}"
                                         RadiusY="{Binding Radius}"/>
                    </Path.Data>
                </Path>
                <Line Stroke="Black"
                      X1="{Binding LineX1}" Y1="{Binding LineY1}"
                      X2="{Binding LineX2}" Y2="{Binding LineY2}"/>
            </Canvas>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>