0

I've got a sample reproduction that crashes despite protection.

It looks like when I try manually serializing a TextRange that contains a custom control that contains a bound DependencyProperty crashes because it fails an assert, because the value of the property isn't assignable to the property, because it's an expression.

Is this a bug in my code somewhere? Is this a bug in the serializer? How can I fix this? How can I make it serialize my custom class in a selection?

I've narrowed it down to this:

Xaml

<Window x:Class="SaveCustomCrash.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:SaveCustomCrash"
    Title="MainWindow" Height="350" Width="525">
  <StackPanel>
    <Button Click="CrashClick">Crash</Button>
    <RichTextBox Name="rtb">
        <FlowDocument>
            <Table>
                <TableRowGroup>
                    <c:CustomRow Settable="{Binding IsVisible,ElementName=rtb}">
                        <TableCell>
                            <Paragraph>Stuff</Paragraph>
                        </TableCell>
                    </c:CustomRow>
                </TableRowGroup>
            </Table>
        </FlowDocument>
    </RichTextBox>
  </StackPanel>
</Window>

Codebehind:

using System.IO;
using System.Windows;
using System.Windows.Documents;

namespace SaveCustomCrash
{
  public partial class MainWindow
  {
    public MainWindow()
    {
        InitializeComponent();
    }

    private void CrashClick(object sender, RoutedEventArgs e)
    {
        rtb.SelectAll();
        using (var memoryStream = new MemoryStream())
        {
            if (!rtb.Selection.CanSave(DataFormats.Xaml))
            {
                MessageBox.Show("Can't Save"); // this doesn't get hit.
                return;
            }

            try
            {
                rtb.Selection.Save(memoryStream, DataFormats.Xaml, true);
            }
            catch // apparently it can't catch this exception.
            {
            }
            memoryStream.Flush();
            memoryStream.Position = 0;
            using (var streamReader = new StreamReader(memoryStream))
            {
                MessageBox.Show("Xaml: " + streamReader.ReadToEnd());
            }
        }
    }
}

public class CustomRow : TableRow
{
    public static readonly DependencyProperty SettableProperty =
        DependencyProperty.Register("Settable", typeof (bool), typeof (CustomRow), new PropertyMetadata(default(bool)));

    public bool Settable
    {
        get { return (bool) GetValue(SettableProperty); }
        set { SetValue(SettableProperty, value); }
    }
  }
}
McKay
  • 12,334
  • 7
  • 53
  • 76

1 Answers1

0

While saving selected text into XAML you can pass last parameter as false which stands for preserveTextElements.

rtb.Selection.Save(memoryStream, DataFormats.Xaml, false);
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185