简体   繁体   中英

c# WPF Graph# layout using DataTemplate to show custom vertices causes XamlParseException

I just recently started working with QuickGraph and Graph# to manage and display graphs of plugins and possible connexions between them.

Each plugin is represented by an object of Plugin class containing properties I would like to show, so i tried to use a DataTemplate for this class.

The problem is when the window in which the GraphLayout is is shown (during InitializeComponent), I get a XamlParseException apparently thrown by the ressource dictionary.

Exception details (sorry for french :P) :

L'exception System.Windows.Markup.XamlParseException s'est produite   HResult=-2146233087   Message=La propriété Set 'System.Windows.ResourceDictionary.DeferrableContent' a levé une exception.   Source=PresentationFramework   LineNumber=0   LinePosition=0   StackTrace:
       à System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       à System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       à System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       à System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       à IHM.PluginsWindow.InitializeComponent() dans c:\SIMON\Ptiou\IHM\00-SRC\widget\PluginsWindow.xaml:ligne 1
       à IHM.PluginsWindow..ctor() dans C:\SIMON\Ptiou\IHM\00-SRC\widget\PluginsWindow.xaml.cs:ligne 35   InnerException: System.NotImplementedException
       HResult=-2147467263
       Message=La méthode ou l'opération n'est pas implémentée.
       Source=PresentationFramework
       StackTrace:
            à System.Windows.Baml2006.Baml2006SchemaContext.ResolveBamlType(BamlType bamlType, Int16 typeId)
            à System.Windows.Baml2006.Baml2006SchemaContext.GetXamlType(Int16 typeId)
            à System.Windows.Baml2006.Baml2006Reader.Process_ConstructorParameterType()
            à System.Windows.Baml2006.Baml2006Reader.Process_OneBamlRecord()
            à System.Windows.Baml2006.Baml2006Reader.ReadKeys()
            à System.Windows.ResourceDictionary.SetDeferrableContent(DeferrableContent deferrableContent)
            à System.Windows.Baml2006.WpfSharedBamlSchemaContext.<>c.<Create_BamlProperty_ResourceDictionary_DeferrableContent>b__297_0(Object target, Object value)
            à System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value)
            à MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
            à MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
       InnerException:

The problem must probably come from the DataTemplate i'm using because the Graph is showing fine without it (can't show any property of the objects though). I also tried setting the graph by binding or from code behind and it didn't change anything.

XAML file where the GraphLayout and the DataTemplate are :

<Window x:Class="IHM.PluginsWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfextensions="clr-namespace:WPFExtensions.Controls;assembly=WPFExtensions"
        xmlns:datamodel="clr-namespace:DATAMODEL;assembly=DATAMODEL"
        xmlns:local="clr-namespace:IHM"
        Height="1080" Width="1920" Margin="0,0,0,0"
        Topmost="true" ResizeMode="NoResize" WindowStyle="None" ShowInTaskbar="False"   
        WindowStartupLocation="Manual"       
        VerticalAlignment="Top"
        HorizontalAlignment="Left"
        AllowsTransparency="True" Background="Transparent"
        WindowState="Normal"
        x:Name="root">


    <Grid Name="containerGrid" Height="1080" Width="1920" Margin="0,0,0,0">
        <Grid.Resources>
            <DataTemplate DataType="{x:Type datamodel:Plugin}">
                <Border BorderThickness="3" CornerRadius="6" Padding="3" BorderBrush="Black">
                    <Grid>
                        <TextBlock Text="{Binding name}"/>
                    </Grid>
                </Border>
            </DataTemplate>
        </Grid.Resources>

        <wpfextensions:ZoomControl>
            <local:MyGraphLayout
                x:Name="pluginGraphLayout"
                LayoutAlgorithmType="FR"
                OverlapRemovalAlgorithmType="FSA"
                HighlightAlgorithmType="Simple">
            </local:MyGraphLayout>
        </wpfextensions:ZoomControl>

    </Grid>

</Window>

CodeBehind :

namespace IHM 
{
    public partial class PluginsWindow : Window, INotifyPropertyChanged
        {
            Point m_start;
            Vector m_startOffset;
            BidirectionalGraph<Plugin, BBlink> pluginGraph = new BidirectionalGraph<Plugin, BBlink>();

            List<Plugin> shownPlugins = new List<Plugin>();
            List<BBlink> shownLinks = new List<BBlink>();

            public PluginsWindow()
            {
                InitializeComponent();
                this.Background = new SolidColorBrush(Color.FromArgb(255, 140, 140, 140));
            }

            public void MakeGraph(List<BBlink> links)
            {
                List<Plugin> plugins = new List<Plugin>();
                foreach(BBlink link in links)
                {
                    if(!plugins.Contains(link.startPlugin))
                        plugins.Add(link.startPlugin);
                    if(!plugins.Contains(link.endPlugin))
                        plugins.Add(link.endPlugin);
                }
                if(plugins.Count < 1)
                    return;

                this.shownPlugins = plugins;
                this.shownLinks = links;
                this.showPathGrid();

                BidirectionalGraph<Plugin, BBlink> graph = new BidirectionalGraph<Plugin,BBlink>();
                foreach (Plugin plugin in plugins)
                    graph.AddVertex(plugin);

                foreach (BBlink link in links)
                    graph.AddEdge(link);

                this.pluginGraph = graph;
                this.pluginGraphLayout.Graph = graph;
            }

            protected void OnPropertyChanged(string propertyName)
            {
                var handle = PropertyChanged;
                if (handle != null)
                    handle(this, new PropertyChangedEventArgs(propertyName));
            }

            public event PropertyChangedEventHandler PropertyChanged;
        }

        public class MyGraphLayout : GraphSharp.Controls.GraphLayout<Plugin, BBlink, IBidirectionalGraph<Plugin, BBlink>>
        { /* nothing to do here... */ }
}

The total inexistance of a Graph# documentation didn't help at all so i tried following this post on their site but it looks identical to what i've done.

Pretty old and unnoticed topic but I'll answer it since I managed to resolve the issue. What i did was create a custom UserControl and having a ControlTemplate in the Window's ressources binding to it.

Main Window's ressources :

<Window.Resources>
    <Style TargetType="{x:Type gxl:VertexControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type gxl:VertexControl}">
                    <Grid>
                        <local:PluginVertexControl DataContext="{TemplateBinding Vertex}">
                        </local:PluginVertexControl>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

Custom UserControl :

<UserControl x:Class="IHM.PluginVertexControl"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="216" d:DesignWidth="244"
             Height="216" Width="244">

    <Border BorderBrush="{Binding color}" CornerRadius="10,10,10,10"
             BorderThickness="{Binding thickness}" Background="LightGray">
        <Grid>
            <TextBlock Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBlock1" Text="{Binding name}" VerticalAlignment="Top" Width="217" FontSize="16" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="12,46,0,0" Name="textBlock2" Text="Format d'entrée :" VerticalAlignment="Top" Width="96" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="12,75,0,0" Name="textBlock3" Text="Format de sortie :" VerticalAlignment="Top" Width="96" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="114,46,0,0" Name="textBlock4" Text="{Binding In}" VerticalAlignment="Top" Width="115" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="114,75,0,0" Name="textBlock5" Text="{Binding Out}" VerticalAlignment="Top" Width="115" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="12,133,0,0" Name="textBlock6" Text="Fonction :" VerticalAlignment="Top" Width="96" />
            <TextBlock Height="47" HorizontalAlignment="Left" Margin="12,153,0,0" Name="textBlock7" Text="{Binding fonction}" VerticalAlignment="Top" Width="217" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="12,104,0,0" Name="textBlock8" Text="Fiabilité (TRL / 10) :" VerticalAlignment="Top" Width="108" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="126,104,0,0" Name="textBlock9" Text="{Binding TRL}" VerticalAlignment="Top" Width="103" />
        </Grid>
    </Border>

</UserControl>

That works just fine. Thank you and farewell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM