Serialize UserControl for xaml but not its children?

Many changes have been made to the XAML serialization stack in .NET 4.0. One of the changes is that when serializing UserControl, you get not only the control itself, but also all of its children.

var sb = new StringBuilder(); var writer = XmlWriter.Create(sb, new XmlWriterSettings { Indent = true, ConformanceLevel = ConformanceLevel.Fragment, OmitXmlDeclaration = true }); var mgr = new XamlDesignerSerializationManager(writer); mgr.XamlWriterMode = XamlWriterMode.Expression; System.Windows.Markup.XamlWriter.Save(this, mgr); return sb.ToString(); 

Instead of getting, for example,

 <MyUserControl xmlns="clr-namespace:MyNamespace" SomeProperty="Add ten thousand child controls" /> 

Now you get

 <MyUserControl xmlns="clr-namespace:MyNamespace" SomeProperty="Add ten thousand child controls"> <StackPanel xmlns="http://microsoft.com/something/xaml/dude"> <TextBlock Text="Child Control ONE!"/> <TextBlock Text="Child Control TWO!"/> <TextBlock Text="Child Control THREE!"/> <!--WTMFH?--> <TextBlock Text="Child Control TEN FRIGGEN THOUSAND!"/> </StackPanel> </MyUserControl/> 

How can I return this behavior back to the original method?

+2
c # serialization wpf user-controls
source share
1 answer

One option is to override ShouldSerializeContent and return false . Still looking for the best answers that will let me point this out of control.

+1
source share

All Articles