GraphSharp Error used in WPF User Control

Jul 8, 2011 at 12:58 PM

Hi, 

Im new to GraphSharp and is currently experiencing the following problem.

I have to add a GraphSharp graph into a Windows Form application. I am using Visual Studio Express 2010. I have downloaded the latest version of GraphSharp. I have used the following tutorial to guide me through the process http://www.switchonthecode.com/tutorials/wpf-tutorial-using-wpf-in-winforms.

I have added the following references:

  • GraphSharp
  • GraphSharp.Control
  • WPFExtensions
  • QuickGraph
  • QuickGraph.Data

When I add the GraphSharp to the XAML file I get the following error:

System.ArgumentException

GenericArguments[1], 'QuickGraph.IEdge`1[System.Object]', on 'GraphSharp.Algorithms.Layout.ILayoutAlgorithm`3[TVertex,TEdge,TGraph]' violates the constraint of type 'TEdge'.

 

System.TypeLoadException
GenericArguments[1], 'QuickGraph.IEdge`1[System.Object]', on 'GraphSharp.Algorithms.Layout.ILayoutAlgorithm`3[TVertex,TEdge,TGraph]' violates the constraint of type parameter 'TEdge'.

The code works when I use it within a WPF application but when I move it to a WPF user control it does not work.

Here is the code that does NOT work:

The section in bold is creating the problem.

<UserControl x:Class="Test.MyWPFControl"
             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="300" d:DesignWidth="300"
             x:Name="root">
 
    <Grid>
        <TabControl>
            <TabItem Header="Test Tab">
                Place holder
            </TabItem>
            <TabItem Header="Search">
                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="30"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="30"></RowDefinition>
                        <RowDefinition Height="30"></RowDefinition>
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="150"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>

                    </Grid.ColumnDefinitions>

                    <TextBlock Text="Enter ID of User :" Grid.Row="0" Grid.Column="0" />
                    <TextBox x:Name="txtName" Grid.Row="0" Grid.Column="1" MinWidth="50"/>
                    <zoom:ZoomControl Grid.ColumnSpan="2" Grid.Row="1">
                    <graphsharp:GraphLayout x:Name="graphlayoutsearch"
                                Graph="{Binding ElementName=root, Path=graphtovisualize}"
                                LayoutAlgorithmType="FR"
                                OverlapRemovalAlgorithmType="FSA"
                                HighlightAlgorithmType="Simple"/>
                    </zoom:ZoomControl>

                    <Button Content="Search" Grid.Row="2" Grid.ColumnSpan="2" Click="Button_Click"/>
                    <Button Content="Show All" Grid.Row="3" Grid.ColumnSpan="2" Click="Button_Click_All"/>
                </Grid>


            </TabItem>

        </TabControl>


    </Grid>
</UserControl>

 

Here is code that works:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:graphsharp="clr-namespace:GraphSharp.Controls;assembly=GraphSharp.Controls"
        xmlns:zoom="clr-namespace:WPFExtensions.Controls;assembly=WPFExtensions"
        Title="MainWindow" Height="550" Width="525"
        x:Name="root"
        >

    <Grid>
        <TabControl>
            <TabItem Header="Test Tab">
                Place holder
            </TabItem>
            <TabItem Header="Search">
                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="30"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="30"></RowDefinition>
                        <RowDefinition Height="30"></RowDefinition>
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="150"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                       
                    </Grid.ColumnDefinitions>

                    <TextBlock Text="Enter ID of User :" Grid.Row="0" Grid.Column="0" />
                    <TextBox x:Name="txtName" Grid.Row="0" Grid.Column="1" MinWidth="50"/>
                    <zoom:ZoomControl Grid.ColumnSpan="2" Grid.Row="1">
                   
                        <graphsharp:GraphLayout x:Name="graphlayoutsearch"
                                Graph="{Binding ElementName=root, Path=graphtovisualize}"
                                LayoutAlgorithmType="FR"
                                OverlapRemovalAlgorithmType="FSA"
                                HighlightAlgorithmType="Simple"/>
                    </zoom:ZoomControl>

                    <Button Content="Search" Grid.Row="2" Grid.ColumnSpan="2" Click="Button_Click"/>
                    <Button Content="Show All" Grid.Row="3" Grid.ColumnSpan="2" Click="Button_Click_All"/>
                </Grid>
               
               
            </TabItem>
 
        </TabControl>


    </Grid>
</Window>

Coordinator
Aug 1, 2011 at 8:48 AM

Please copy here the code of your code-behind file (especially the declaration of the graphtovisualize variable ) .

Aug 10, 2011 at 3:03 PM

Hi.

Thanks for the help on this matter.

Please find the code for the graphtovisualize:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QuickGraph;
using System.Collections;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        //graph that display all relationships
        private IBidirectionalGraph<Object,IEdge<object>> _graphtovisualize;

        public IBidirectionalGraph<Object, IEdge<object>> graphtovisualize
        {
            get { return _graphtovisualize; }
        }

      

        //default constructor
        public MainWindow()
        {
            CreateGraphToVisualize();           //create graph
            InitializeComponent();              //start the different components

        }


        /// <summary>
        /// Create graph with values from the database
        /// </summary>
        private void CreateGraphToVisualize()
        {
           
              //create graph object
            var h = new BidirectionalGraph<object, IEdge<object>>();

            //populate the array with the userid's from the database
            string[] vertices = new string[5];

            //Calcualte the relationships from the database
            for (int i = 0; i < 5; i++)
            {
                vertices[i] = i.ToString();
                //add vertices to graph aka the nodes
                h.AddVertex(vertices[i]);
              
            }
 
            //add some edges

            h.AddEdge(new Edge<object>(vertices[0], vertices[2]));
            h.AddEdge(new Edge<object>(vertices[1], vertices[4]));
            h.AddEdge(new Edge<object>(vertices[0], vertices[1]));
            h.AddEdge(new Edge<object>(vertices[1], vertices[3]));
            h.AddEdge(new Edge<object>(vertices[2], vertices[3]));

           _graphtovisualize = h;

        }
    }
}

 

Aug 14, 2011 at 10:49 AM
Hi palesz.

I have added the requested code.

I was abroad for few weeks. Went to BlackHat and Defcon conferences in Las Vegas.

I am not sure how to continue with the current problem I have, hence your help will be useful on the matter. Please let me know if I should provide you with more information.

Kind regards.
Aubrey