Search code examples
wpfserializationcommandbinding

Stop CommandBindings being serialised in UserControl


I have a user control that programmatically sets up its command bindings and content.

I also serialise this control to XAML. I do not want the content or bindings to be serialised since I set these up. Content is taken care of by overriding:

public virtual bool ShouldSerializeContent()

And I was pleased to see an equivalent for command bindings:

public bool ShouldSerializeCommandBindings()

However, this function is not virtual, and hiding it by specifying new in my implementation appears to do nothing? This does appear to be the recommended way to use it according to this MS page: http://msdn.microsoft.com/en-us/library/53b8022e(v=vs.85).aspx

I have also tried shadowing the CommandBindings property and using [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)], but this just breaks my bindings.

Can anyone show me the correct way? Is this a bug?


Solution

  • Aha, nevermind I've sorted it. The trick is indeed to shadow the property, but I wasn't providing any implementation. The following works to turn off serialization:

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public CommandBindingCollection CommandBindings 
    {
        get
        {
            return base.CommandBindings;
        }
    }