C#WPF图形#使用DataTemplate显示自定义顶点的布局导致XamlParseException



我刚刚开始使用QuickGraph和Graph#来管理和显示插件的图形以及它们之间可能的连接。

每个插件都由我想显示的属性的插件类的对象表示,因此我尝试使用此类的dataTemplate。

问题是在显示绘图layout的窗口时(在initializecomponent期间),我显然得到了XamlParseException,显然是由ressource dictionary抛出的。

例外细节(对不起,法语: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:SIMONPtiouIHM0-SRCwidgetPluginsWindow.xaml:ligne 1
       à IHM.PluginsWindow..ctor() dans C:SIMONPtiouIHM0-SRCwidgetPluginsWindow.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:

问题可能必须来自我正在使用的DataTemplate,因为没有它的图形很好(虽然不能显示对象的任何属性)。我还尝试通过绑定或从后面的代码设置图形,但没有更改任何内容。

XAML文件,其中GraphLayout和DatateMplate是:

<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... */ }
}

图#文档的总不存在无济于事,因此我尝试关注他们的网站上的这篇文章,但看起来与我所做的相同。

非常古老且没有注意到的话题,但是我会回答它,因为我设法解决了这个问题。我所做的是创建一个自定义的usercontrol,并在窗口的ressources绑定到它。

主窗口的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>

自定义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>

效果很好。谢谢你并告别。

最新更新