Our tester threw curly brackets at our persisting WPF RichTextBoxes. On save and reopen, there are magically more curly brackets.
I've condensed the issue / code down.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<RichTextBox x:Name="rtb1" />
<Button Grid.Row="1" Click="Button_Click">Draw a fish</Button>
<RichTextBox x:Name="rtb2" Grid.Row="2"/>
</Grid>
</Window>
Two rich text boxes. On button click, the bottom one gets set to the result of the first one after persist and restore.
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
rtb1.Document = new FlowDocument(new Paragraph(new Run("{")));
}
public static FlowDocument CreateFlowDocumentFromByteArray(byte[] byteArray)
{
return (FlowDocument)XamlReader.Load(new MemoryStream(byteArray));
}
public static byte[] CreateByteArrayFromFlowDocument(FlowDocument flowDocument)
{
MemoryStream mStream = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = false;
settings.OmitXmlDeclaration = true;
XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(mStream, settings));
dsm.XamlWriterMode = XamlWriterMode.Value;
XamlWriter.Save(flowDocument, dsm);
mStream.Close();
return mStream.ToArray();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
rtb2.Document = CreateFlowDocumentFromByteArray(CreateByteArrayFromFlowDocument(rtb1.Document));
}
}
}
Why is this happening? How do we stop it?