UserControl x:Class="WpfApplication1.MinimalControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300"> <TextBlock x:Name="Fred" /> </UserControl>Used in this form:
<Window x:Class="WpfApplication1.MinimalForm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:uc="clr-namespace:WpfApplication1" Title="MinimalForm" Height="300" Width="300"> <StackPanel> <Canvas x:Name="ACanvas"> <uc:MinimalControl /> <uc:MinimalControl /> </Canvas> <Button Click="Button_Click" >Try</Button> </StackPanel> </Window>Here's the test code behind:
using System.Windows; using System.Windows.Controls; namespace WpfApplication1 { /// <summary> /// Interaction logic for MinimalForm.xaml /// </summary> public partial class MinimalForm : Window { public MinimalForm() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { string s = System.Windows.Markup.XamlWriter.Save(ACanvas); System.IO.StringReader stringReader = new System.IO.StringReader(s); System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader); //Exception here: Canvas x = (Canvas)System.Windows.Markup.XamlReader.Load(xmlReader); } } }Any way of manipulating the NameScope (or something) to avoid this ?