Dynamic Insertion of XAML?

Is the following scenario possible:

  1. User pastes the XAML declaration of a button into a TextBlock.
  2. App takes XAML and inserts it into a ContentControl defined at compile-time.
  3. Visual tree is updated to render the button.
[266 byte] By [Leebert] at [2007-12-23]
# 1

To add a canvas inside another canvas use this code:

Stream s = File.OpenRead("test.xaml");
Canvas newCanvas = (Canvas)XamlReader.Load(s);
s.Close();

parentCanvas.Child = newCanvas;

test.xaml looks like this:

<Canvas
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

<Button>Hej</Button>

</Canvas>

You can probably figure out how to use the code above to fit your needs.

Chimme at 2007-8-30 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2
Coolness, works like a champ! Many thanks.
Leebert at 2007-8-30 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3
In case you want to bind your UI to dynamically loaded XAML, this blog entry of mine demonstrates how to do that: http://www.infusionblogs.com/blogs/jsmith/archive/2006/07/30/686.aspx It's amazing how flexible WPF is! :)
JoshSmith at 2007-8-30 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4
Thanks for the help everyone. It seems to be working pretty nicely.

Is there a recommended way to format the output of XAMLWriter.Save? Right now, I'm doing this:

myTextBox.Text = (string)XamlWriter.Save(TargetElement);

and it spits out a single line of text. Will I need to do brute-force parsing based on "<" and ">"?

Leebert at 2007-8-30 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified