We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How can i define ports for a SymbolViewModel?

I have created a SymbolViewModel to use as a source for the stencil, but i cant define the ports location as i do with NodeViewModel si not have a Port atribute.

My code is:


SymbolViewModel symbol1 = symbol_square();

            (stencil.SymbolSource as SymbolCollection).Add(symbol1);



public SymbolViewModel symbol_square()

        {

            ResourceDictionary node_shape = (ResourceDictionary)Application.LoadComponent(new Uri(";component/Nodes_shapes.xaml", UriKind.Relative));

            string nome = "symbol1";

            var symbol = new Syncfusion.UI.Xaml.Diagram.Stencil.SymbolViewModel()

            {

               SymbolTemplate = (DataTemplate)node_shape["Rectangle"],

                Symbol = (DataTemplate)node_shape["Rectangle"],

                Key = nome,

            };

            NodePortViewModel port1 = new NodePortViewModel()

            {

                NodeOffsetX = 0,

                NodeOffsetY = 0.5,

            };

return symbol;

        }





1 Reply 1 reply marked as answer

DT Deepa Thiruppathy Syncfusion Team October 28, 2022 05:07 AM

Hi Rodrigo,


Requirement: How to add ports to stencil symbols


Stencil symbols do not have direct support to have ports with it. Only diagram objects node, Connector can have support to have ports. You can add the ports to symbols when it is being added to the diagram as a node using ItemAddedEvent.


Code snippet:


(diagram.Info as IGraphInfo).ItemAdded += MainWindow_ItemAdded;

private void MainWindow_ItemAdded(object sender, ItemAddedEventArgs args)

{

    if (args.Item is NodeViewModel && args.OriginalSource is SymbolViewModel && args.ItemSource == ItemSource.Stencil)

    {

        string symbolName = (args.OriginalSource as SymbolViewModel).Symbol.ToString();

        if (symbolName.Equals("User"))

        {

            NodeViewModel node = args.Item as NodeViewModel;

            node.Ports = new PortCollection()

            {

                 new NodePortViewModel(){NodeOffsetX = 0.5, NodeOffsetY = 0}

            };

        }

        else if (symbolName.Equals("Diamond"))

        {

            NodeViewModel node = args.Item as NodeViewModel;

            node.Ports = new PortCollection()

            {

                 new NodePortViewModel(){NodeOffsetX = 0.5, NodeOffsetY = 0.5}

            };

        }

    }

}


We have added ports to the symbol nodes based on its Symbol’s value. We have created a simple sample based on your requirement.


Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/StencilSymbolWithPorts-1090335502


Regards,

Deepa Thiruppathy


Note: If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Marked as answer
Loader.
Live Chat Icon For mobile
Up arrow icon