Articles in this section
Category / Section

How to do Expand/Collapse for MultiParent Layout in WPF Diagram(SfDiagram)?

2 mins read

WPF Diagram (SfDiagram) supports Expand and Collapse operations for single parent tree layouts. For the multiparent layout, you need to add some custom code for the Expand and Collapse operation. We have prepared a simple sample for the Expand and Collapse operations in a multiparent layout. Please find the sample and custom code as follows.

C# 

 
ObservableCollection<object> SelectedNodeCollection => (sfdiagram.SelectedItems as SelectorViewModel)?.Nodes as ObservableCollection<object>;
 
private ObservableCollection<NodeViewModel> NodeCollection => sfdiagram.Nodes as ObservableCollection<NodeViewModel>;
 
public ObservableCollection<ItemInfo> AllCards => sfdiagram.DataSourceSettings.DataSource as ObservableCollection<ItemInfo>;
 
private ObservableCollection<ConnectorVM> ConnectorsCollection => sfdiagram.Connectors as ObservableCollection<ConnectorVM>;
private void ExpandCollapse_Click(object sender, RoutedEventArgs e)
{
    var workingCollection = SelectedNodeCollection;
    var selectednode = (sfdiagram.SelectedItems as SelectorViewModel).SelectedItem;
    ItemInfo obj = null;
    if(selectednode != null && selectednode is NodeViewModel)
    {
        obj = (ItemInfo)(selectednode as NodeViewModel).Content;
    }
 
    if (obj != null)
    {
        workingCollection = new ObservableCollection<object> 
        {
            NodeCollection.FirstOrDefault(j => j.Content == obj) 
        };
    }
    if (workingCollection.Count <= 0) return;
    //Multiple Nodes can be selected, and their child node can be collapsed/expanded at the same time
    foreach (NodeViewModel selectedItem in workingCollection)
    {
        var expandCollapseParameter = new ExpandCollapseParameter
        {
            Node = selectedItem,
            IsUpdateLayout = true
        };
        var graphInfo = sfdiagram.Info as IGraphInfo;
        // Without this get operation, the IsExpanded property of the node is not updated.
        var isExpanded = selectedItem.IsExpanded; 
        graphInfo?.Commands.ExpandCollapse.Execute(expandCollapseParameter);
 
        var currentCardTree = new List<ItemInfo>();
        var content = selectedItem.Content as ItemInfo;
        currentCardTree.AddRange(FindChildren(AllCards.ToList(), content));
 
        var childNodes = NodeCollection.Where(j => currentCardTree.Contains(j.Content)).ToList();
        var childConnectors = ConnectorsCollection.Where(j => childNodes.Contains(j.TargetNode)).ToList();
        childConnectors.ForEach(j => j.IsVisible = !isExpanded);
    }
}private List<ItemInfo> FindChildren(List<ItemInfo> source, ItemInfo root)
{
    return source.Where(j => j.ReportingPerson.Contains(root.Name))
        .Union(source.Where(j => j.ReportingPerson.Contains(root.Name)).SelectMany(j => FindChildren(source, j)))
        .ToList();
}

 

View sample in GitHub

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied