Dynamic Insertion of XAML?
- User pastes the XAML declaration of a button into a TextBlock.
- App takes XAML and inserts it into a ContentControl defined at compile-time.
- Visual tree is updated to render the button.
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.
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 ">"?