1

Is there any xaml serialization attribute that I can specify for a dependency property which actually is a collection (TextDecorationCollection)?

I want to use serialization for cloning a very large and complex object. Here a sample of the code, simplified:

There is a MyVisualObject, that contains a lot of properties, including a custom font, which I want to clone

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class Export : Attribute
{
}

public class MyVisualObject : DependencyObject
{
    [Export]
    public CustomFont Font
    {
        get { return (CustomFont)GetValue(FontProperty); }
        set { SetValue(FontProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Font.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FontProperty =
        DependencyProperty.Register("Font", typeof(CustomFont), typeof(MyVisualObject));

    public MyVisualObject()
    {
        this.Font = new CustomFont();
    }
}

And the custom font is defined like this:

public class CustomFont : DependencyObject
    {
        public TextDecorationCollection Decorations
        {
            get { return (TextDecorationCollection)GetValue(DecorationsProperty); }
            set { SetValue(DecorationsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TextDecorations.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DecorationsProperty =
            DependencyProperty.Register("Decorations", typeof(TextDecorationCollection), typeof(CustomFont), new UIPropertyMetadata(new TextDecorationCollection()));

        public CustomFont()
        {
            this.Decorations = System.Windows.TextDecorations.Underline;
        }
    }

THe deep clone method:

public static T DeepClone<T>(T from)
        {
            object clone = Activator.CreateInstance(from.GetType());

            Type t = from.GetType();
            System.Reflection.PropertyInfo[] pinf = t.GetProperties();

            foreach (PropertyInfo p in pinf)
            {
                bool serialize = false;

                foreach (object temp in p.GetCustomAttributes(true))
                {
                    if (temp is Export)
                    {
                        serialize = true;
                    }
                }

                if (serialize)
                {
                    string xaml = XamlWriter.Save(p.GetValue(from, null));                        
                    XmlReader rd = XmlReader.Create(new StringReader(xaml));
                    p.SetValue(clone, XamlReader.Load(rd), null);
                }                
            }

            return (T)clone;
        }

The problem is that each time I initialize the Decorations as Underline

this.Decorations = System.Windows.TextDecorations.Underline;

the cloning process crashes with this error:'Add value to collection of type 'System.Windows.TextDecorationCollection' threw an exception.' Line number '1' and line position '213'.

As far as I found out, the serialization, which is this part

string xaml = XamlWriter.Save(p.GetValue(from, null));

returns an xaml which does not have the decorations set as a collection:

<CustomFont xmlns="clr-namespace:WpfApplication1;assembly=WpfApplication1" xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <CustomFont.Decorations>
        <av:TextDecoration Location="Underline" /> 
    </CustomFont.Decorations>
</CustomFont>

But the clone process would work if the xaml would be like this:

<CustomFont xmlns="clr-namespace:WpfApplication1;assembly=WpfApplication1" xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <CustomFont.Decorations>
        <av:TextDecorationCollection>
            <av:TextDecoration Location="Underline" /> 
        </av:TextDecorationCollection>
    </CustomFont.Decorations>
</CustomFont>

I found a workaround, something with string replacements:

xaml = xaml.Replace("<CustomFont.Decorations><av:TextDecoration Location=\"Underline\" /></CustomFont.Decorations>", "<CustomFont.Decorations><av:TextDecorationCollection><av:TextDecoration Location=\"Underline\" /></av:TextDecorationCollection></CustomFont.Decorations>");

but I think it's really dirty, and I would apreciate it if you could provide a more clean solution (specifying an attribute for the Decorations property for example)

melculetz
  • 1,961
  • 8
  • 38
  • 51

1 Answers1

0

Have you tried applying the following attribute to the Decorations property:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
Joe Amenta
  • 4,662
  • 2
  • 29
  • 38