ItemsControl +生成项之间的连接线

时间:2014-05-19 12:37:28

标签: wpf diagramming

我想绘制连接由ItemsControl绘制的块的线条。这些是我的课程:

public class NodeBase
{
    public string Name { get; set; }
    public double X { get; set; }
    public double Y { get; set; }
    public List<Connector> Inputs { get; set; }
    public List<Connector> Outputs { get; set; }
}

public class Connector
{
    public string Name { get; set; }
    public List<Connection> Connections { get; set; }
}

public class Connection
{
    public NodeBase TargetBlock { get; set; }
}

然后我将List设置为ItemsControls ItemsSource。

这是我的每个NodeBase项目的DataTemplate:

<DataTemplate DataType="{x:Type local:NodeBase}">
    <Border BorderBrush="Black" BorderThickness="1">
        <StackPanel>
            <DockPanel>
                <TextBlock Text="{Binding Name}" TextAlignment="Center" Foreground="White" Margin="30, 0, 30, 0" DockPanel.Dock="Top"/>
                <Grid Background="Gray">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50*"/>
                        <ColumnDefinition Width="50*"/>
                    </Grid.ColumnDefinitions>
                    <local:InputItemsControl ItemsSource="{Binding Inputs}" Grid.Column="0"/>
                    <local:OutputItemsControl ItemsSource="{Binding Outputs}" Grid.Column="1" Margin="5, 0, 0, 0"/>
                </Grid>
            </DockPanel>
        </StackPanel>
    </Border>
</DataTemplate>

OutputItemsControl和InputItemsControl是ItemsControls。 OutputItemsControl中的连接器的DataTemplate如下所示:

<DataTemplate DataType="{x:Type local:Connector}">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
        <TextBlock Text="{Binding Name}" HorizontalAlignment="Right"/>
        <Rectangle Fill="Black" Width="5" Height="5" HorizontalAlignment="Right" Margin="5, 0, 0, 0" Name="_ConnectorRectangle"/>
    </StackPanel>
</DataTemplate>

我的块被正确绘制。 现在我想在名为“_ConnectorRectangle”的矩形之间绘制一条直线来连接可视节点表示。 任何想法我怎样才能实现它?

0 个答案:

没有答案