Infragistics:如何动态添加数据并使用 ForceDirectedGraphDiagramLayout?

Infragistics: How do I Dynamically add data and use ForceDirectedGraphDiagramLayout?

我有一个图绑定到 ObservableCollection 个节点和 ObservableCollection 个连接。当节点和连接被硬编码到这些列表中时,图表可以很好地显示它们。但是,当稍后在程序中将节点和连接值添加到列表中时,所有节点都将在 window 的中心放置在另一个节点之上。在这种情况下,ForceDirectedGraphDiagramLayout 似乎没有像我想象的那样工作。我的问题是,如何动态地将数据添加到图表中并使图表位于 ForceDirectedGraphDiagramLayout?

这是我的网格:

<Grid Background="White">
    <ig:XamDiagram x:Name="Diagram"
                   OptionsPaneVisibility="Visible" 
                   NavigationPaneVisibility="Visible" 
                   DefaultDragInteraction="Pan">
        <ig:XamDiagram.NodeDefinitions>
            <ig:NodeDefinition TargetType="{x:Type models:EntityType}" 
                               KeyMemberPath="Name"
                               ConnectionPointsMemberPath="EntityPoints" >
                <ig:NodeDefinition.NodeStyle>
                    <Style TargetType="ig:DiagramNode" >
                        <Setter Property="DisplayTemplate" Value="{StaticResource NodeTemplate}"/>
                        <Setter Property="ShapeType" Value="None" />
                        <Setter Property="Fill" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.FillColor, Converter={StaticResource con}, Mode=TwoWay}" />
                        <Setter Property="Width" Value="140" />
                    </Style>
                </ig:NodeDefinition.NodeStyle>
            </ig:NodeDefinition>
        </ig:XamDiagram.NodeDefinitions>
        <ig:XamDiagram.ConnectionDefinitions>
            <ig:ConnectionSourceDefinition TargetType="{x:Type models:Association}" 
                                           StartNodeKeyMemberPath="End1Name" 
                                           EndNodeKeyMemberPath="End2Name"
                                           StartNodeConnectionPointNameMemberPath="PointFromName"
                                           EndNodeConnectionPointNameMemberPath="PointToName">
                <ig:ConnectionSourceDefinition.ConnectionStyle>
                    <Style TargetType="ig:DiagramConnection">
                        <Setter Property="StartCapType" Value="None"/>
                        <Setter Property="EndCapType" Value="FilledArrow"/>
                        <Setter Property="DisplayTemplate" >
                            <Setter.Value>
                                <DataTemplate/>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ig:ConnectionSourceDefinition.ConnectionStyle>
            </ig:ConnectionSourceDefinition>
        </ig:XamDiagram.ConnectionDefinitions>
        <ig:XamDiagram.Layout>
            <ig:ForceDirectedGraphDiagramLayout Buffer="200" />
        </ig:XamDiagram.Layout>
    </ig:XamDiagram>
</Grid>

后面的代码:

    private CurrentData currentData = CurrentData.GetInstance();

    private ObservableCollection<EntityType> Nodes { get; set; }
    private ObservableCollection<Association> Associations { get; set; }

    public MyDiagram()
    {
        InitializeComponent();

        Nodes = currentData.GetNodes();
        Associations = currentData.GetAssociations();

        Diagram.ItemsSource = Nodes;
        Diagram.ConnectionsSource = Associations;
    }

    void OnLoad(object sender, RoutedEventArgs e)
   {
       Diagram.ScaleToFit();
   }

GetNodes()GetAssociations() return 各自的 ObservableCollections。当这些集合中的对象在程序运行之前被硬编码时,图表定位工作完美。当程序已经 运行 时将对象添加到列表中时,所有节点都放在彼此之上。

我通过将数据绑定从代码隐藏构造函数移动到 OnLoad() 函数解决了这个问题。

后面的代码现在看起来像这样:

private CurrentData currentData = CurrentData.GetInstance();

private ObservableCollection<EntityType> Nodes { get; set; }
private ObservableCollection<Association> Associations { get; set; }

public MyDiagram()
{
    InitializeComponent();
}

void OnLoad(object sender, RoutedEventArgs e)
{
    Nodes = currentData.GetNodes();
    Associations = currentData.GetAssociations();

    Diagram.ItemsSource = Nodes;
    Diagram.ConnectionsSource = Associations;
    Diagram.ScaleToFit();
}