1

I have the problem that I would like to render the contents of a variable (referenced via the expression "Metatag.Configuration[VisualizerCode].Value" in the code below). The variable contains xaml code (as a string), for example the following contents:

<Grid>
<Canvas> 
  <Border Canvas.Top="0" Canvas.Left="390" Width="50" Height="100" BorderThickness="2" BorderBrush="Black"> </Border>
  <Border Canvas.Top="100" Canvas.Left="340" Width="100" Height="50" BorderThickness="2" BorderBrush="Black"> </Border>
</Canvas>
</Grid>

In my application I have a Grid, in which I would like to render the contents of the variable:

<Grid Margin="0,10,0,0" Visibility="Visible">
  <ContentControl Content="{Binding Path=Metatag.Configuration[VisualizerCode].Value}">
</ContentControl>

Unfortunately, if I run this, the string (= uninterpreted contents of the variable) is printed as text in the Grid, instead of being interpreted (in which case 2 nice, simple borders should be drawn).

How can I get XAML to interpret the contents of the variable and render it ?

Thanks !

Woelund

Woelund
  • 25
  • 5
  • possible duplicate of [Loading XAML at runtime?](http://stackoverflow.com/questions/910814/loading-xaml-at-runtime) – Janis F Nov 17 '14 at 15:01

1 Answers1

1

You can try using some custom Converter to convert (parse) the string to some instance of the Grid:

public class StringToElementConverter : IValueConverter {
   public object Convert(object value, Type targetType, object parameter,
                                       CultureInfo culture){
      var pc = new ParserContext();
      pc.XmlnsDictionary[""] = 
                   "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
      pc.XmlnsDictionary["x"] = "http://schemas.microsoft.com/winfx/2006/xaml";
      return XamlReader.Parse(System.Convert.ToString(value), pc);
   }
   public object ConvertBack(object value, Type targetType, object parameter, 
                                           CultureInfo culture){
      throw new NotImplementedException();
   }
}

Declare the converter as some resource and used for the Binding in XAML code:

<Window.Resources>
   <local:StringToElementConverter x:Key="elementConverter"/>
</Window.Resources>

<ContentControl Content="{Binding Metatag.Configuration[VisualizerCode].Value,
                          Converter={StaticResource elementConverter}}"/>

I hope you know how to declare the prefix local representing the local namespace in which your converter class is declared.

King King
  • 61,710
  • 16
  • 105
  • 130
  • 1
    Incredible ... I am officially impressed ! Thanks a lot - works like a charm !! – Woelund Nov 17 '14 at 15:27
  • You can just directly use `XamlReader.Parse` instead of creating a `ParserContext` and define your own namespaces, including the default namespaces.. – 123 456 789 0 Nov 17 '14 at 16:20
  • @lll I've experienced with `XamlReader.Parse`, without using `ParserContext` I have to include the `xmlns` ***right inside the string***, such as like this `"..."`. You can try a simple demo yourself. – King King Nov 17 '14 at 16:25
  • @KingKing I can see it not being added to the `Child` tags but the `Root` tag will have the namespaces. – 123 456 789 0 Nov 17 '14 at 16:25
  • @lll I don't understand what you mean. The problem here is using ParserContext or not, if using it, do as what in my answer, if don't use it, do as what in my comment above. – King King Nov 17 '14 at 16:26
  • @KingKing My point was you can use `XamlReader.Parse` without handling your own `ParserContext`, then you pointed that you experienced a problem where you have to add the `namespace` inside the tags. Which I know that you don't need to do it if it's the root tag. If it's the child tag then no, it won't have the namespaces. That's how I understood your problem. – 123 456 789 0 Nov 17 '14 at 16:31
  • @lll please read the OP's question again, his input string is ` ` – King King Nov 17 '14 at 16:32
  • @KingKing I wasn't referring to the OP's question. I would suggest modifying his string to add those namespaces then get rid of `ParserContext`. – 123 456 789 0 Nov 17 '14 at 16:39