Dynamically loading a DataTemplate for a gallery

Ribbon for WPF Forum

Posted 11 years ago by dmelinosky
Avatar
Anyone ever try to dynamically load a DataTemplate for a RibbonGallery? I can dynamically create the template and load it with XamlReader, but as soon as I try to reference the Actipro Ribbon Common Dictionary, I get an exception.

Here's some code

           XElement element = new XElement("DataTemplate",
                                                            new XElement("Border",
                                                                new XAttribute("Width", "75"),
                                                                new XAttribute("BorderThickness", "2"),
                                                                new XAttribute("BorderBrush", "{DynamicResource {x:Static themes:RibbonCommonDictionary.WindowBackgroundBrushKey}}"),
                                                                new XAttribute("CornerRadius", "2"),
                                                                new XAttribute("Padding", "10"),
                                                                new XElement("StackPanel",
                                                                    new XElement("TextBlock",
                                                                        new XAttribute("Text", "{Binding Path=Notation}")),
                                                                    new XElement("TextBlock",
                                                                        new XAttribute("Text", "{Binding Path=Precision}")),
                                                                    new XElement("TextBlock",
                                                                        new XAttribute("Text", "{Binding Path=Overwrite}")))));


            string xaml = element.ToString();

            MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));

            ParserContext pc = new ParserContext();

            pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");

            pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");

            pc.XmlnsDictionary.Add("themes", "clr-namespace:ActiproSoftware.Windows.Controls.Ribbon.Themes;assembly=ActiproSoftware.Ribbon.WPF30");

            DataTemplate datatemplate = (DataTemplate)XamlReader.Load(sr, pc);

            return datatemplate;
An exception is thrown when I get to the XamlReader.Load method. Anyone ever try anything similar?

Comments (5)

Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
What exactly is the exception you get (all details) and are you sure that you have the Ribbon assembly loaded prior to running this code?


Actipro Software Support

Posted 11 years ago by dmelinosky
Avatar
I'll get that information for you on Tuesday ( what with Memorial Day and all :) )
Posted 11 years ago by dmelinosky
Avatar
The exception information from VS is below

System.Windows.Markup.XamlParseException occurred
Message="Cannot find type 'RibbonCommonDictionary'. Type names are case sensitive. Error at Line 2 Position 22."
Source="PresentationFramework"
LineNumber=2
LinePosition=22
StackTrace:
at System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition, Uri baseUri, XamlObjectIds currentXamlObjectIds, XamlObjectIds contextXamlObjectIds, Type objectType)
InnerException:


Yes, the ribbon is definately loaded at this point. I am trying to dynamically add the template, but maybe there is another way to do this, by loading a template from a resource dictionary? Can that be done?
Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
If you want to throw together a simple sample project and email it over, we can debug it to see if we notice anything that may help. If everything is set up appropriately and assemblies loaded, I would think it should work.

Sure you can load templates from resource dictionaries.


Actipro Software Support

Posted 9 years ago by Sascha Nitschke
Avatar
I had a simular error. The problem is a setting in Visual Studio (in my case 2008).

Look here: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/34993a9b-3006-4277-bd6d-ce76b1c9ed05

Greeting
Sascha
The latest build of this product (v2018.1 build 0675) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.