Graph in a UserControl

Oct 9, 2013 at 2:35 PM
Hello, I am new to Graph sharp, and I am having some problems, I have done the Simple usage scenario tutorial with great results, but what I want to do is to create a UserControl that will have the graph and the put this UserControl inside a WPF Window, here is my code for the UserControl:
<UserControl x:Class="LMS_Pipeline_configuration.LMSPipeGraph"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:graphsharp="clr-namespace:GraphSharp.Controls;assembly=GraphSharp.Controls"
             xmlns:zoom="clr-namespace:WPFExtensions.Controls;assembly=WPFExtensions" 
             mc:Ignorable="d" 
             d:DesignHeight="350" d:DesignWidth="525">
    <Grid>
        <graphsharp:GraphLayout x:Name="graphlayout"
                                Graph="{Binding ElementName=root, Path=GraphToVisualize}" 
                                LayoutAlgorithmType="EfficientSugiyama"
                                OverlapRemovalAlgorithmType="FSA"
                                HighlightAlgorithmType="Simple"/>
    </Grid>
</UserControl>
With this code behind:
namespace LMS_Pipeline_configuration
{
    /// <summary>
    /// Lógica de interacción para LMSPipeGraph.xaml
    /// </summary>
    public partial class LMSPipeGraph : UserControl
    {
        private IBidirectionalGraph<object, IEdge<object>> _graphToVisualize;

        public IBidirectionalGraph<object, IEdge<object>> GraphToVisualize
        {
            get { return _graphToVisualize; }
        }

        public LMSPipeGraph()
        {
            createGraphToVisualize();
            InitializeComponent();
        }

        private void createGraphToVisualize()
        {
            var g = new BidirectionalGraph<object, IEdge<object>>();

            string[] vertex = new string[5];
            for (int i = 0; i < 5; i++)
            {
                vertex[i] = i.ToString();
                g.AddVertex(vertex[i]);
            }

            //add some edges
            g.AddEdge(new Edge<object>(vertex[0], vertex[1]));
            g.AddEdge(new Edge<object>(vertex[1], vertex[2]));
            g.AddEdge(new Edge<object>(vertex[2], vertex[3]));
            g.AddEdge(new Edge<object>(vertex[3], vertex[1]));
            g.AddEdge(new Edge<object>(vertex[1], vertex[4]));
            _graphToVisualize = g;
        }
    }
}
it's the same code from the tutorial, and this is the Window where I want to insert it:
<Window x:Class="LMS_Pipeline_configuration.pruebas"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:LMS_Pipeline_configuration"
        Title="pruebas" Height="300" Width="300">
    <Grid>
        <local:LMSPipeGraph/>
    </Grid>
</Window>
with this code behind
namespace LMS_Pipeline_configuration
{
    /// <summary>
    /// Lógica de interacción para pruebas.xaml
    /// </summary>
    public partial class pruebas : Window
    {
        public pruebas()
        {
            InitializeComponent();
        }
    }
}
When I run the application I see the window but there is no graph there, I did some debugging and the createGraphToVisualize is executed and it seems to work fine, but I can't see the graph.

Do anyone know what the error is?
Thanks a lot!
Oct 10, 2013 at 7:50 AM
Hi, try to initialize GraphSharp on UserControl Loaded event instead of the constructor.

Btw, take a look at my enchanced graph library : https://graphx.codeplex.com/
Oct 10, 2013 at 2:08 PM
x:Name="root"
In the xaml file. I will have a look to your library
Thanks