Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As Jakob Christensen noted, you can load any XAML you want using <code>XamlReader.Load</code>. This doesn't apply only for styles, but <code>UIElement</code>s as well. You just load the XAML like:</p> <pre><code>UIElement rootElement; FileStream s = new FileStream(fileName, FileMode.Open); rootElement = (UIElement)XamlReader.Load(s); s.Close(); </code></pre> <p>Then you can set it as the contents of the suitable element, e.g. for</p> <pre><code>&lt;Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Foo Bar"&gt; &lt;Grid x:Name="layoutGrid"&gt; &lt;!-- any static elements you might have --&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>you could add the <code>rootElement</code> in the <code>grid</code> with:</p> <pre><code>layoutGrid.Children.Add(rootElement); layoutGrid.SetColumn(rootElement, COLUMN); layoutGrid.SetRow(rootElement, ROW); </code></pre> <p>You'll naturally also have to connect any events for elements inside the <code>rootElement</code> manually in the code-behind. As an example, assuming your <code>rootElement</code> contains a <code>Canvas</code> with a bunch of <code>Path</code>s, you can assign the <code>Path</code>s' <code>MouseLeftButtonDown</code> event like this:</p> <pre><code>Canvas canvas = (Canvas)LogicalTreeHelper.FindLogicalNode(rootElement, "canvas1"); foreach (UIElement ui in LogicalTreeHelper.GetChildren(canvas)) { System.Windows.Shapes.Path path = ui as System.Windows.Shapes.Path; if (path != null) { path.MouseLeftButtonDown += this.LeftButtonDown; } } </code></pre> <p>I've not tried switching XAML files on the fly, so I cannot say if that'll really work or not.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload