0

For illustrative purposes, say I wanted to create a subclass of the WPF TextBox called BizarroTextBox where the IsReadOnly property has the reverse effect of its default behavior.

Now normally in the non-WPF world, for a hypothetical 'TextBox' control, it would be relatively trivial...

public class BizarroTextBox : TextBox
{
    public override bool IsReadOnly
    {
        get{ return !base.IsReadOnly; }
        set{ base.IsReadOnly = !value; }
    }
}

...but I'm not sure how to/if it's even possible to do something similar when dealing with DependencyObjects and DependencyProperties.

While I could re-define the property's metadata in my subclass and use a Coalesce function to invert the value on the way in, I don't know of a way to invert it on the way back out.

You also can't simply use an bool-inverting converter either as there's only one object which can't be both the source and target unless you're connecting two different properties, which we're not.

You can't do it in the CLR getter/setters either as they are convenience methods which are bypassed when using bindings.

It would be so much easier if they had a coalesce-on-the-way-back-out method but I'm not aware of any such thing. So can this be done?

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • After thinking about this some more, I'm even more convinced this isn't possible as in my above example, the modifications are happening in the getter/setter, not the actual store (the base's property value) whereas with a DependencyProperty, I'm pretty sure you're modifying the stored value directly. – Mark A. Donohoe Sep 13 '13 at 02:28
  • I'm not sure in your `Non WPF` example would work as `IsReadOnly` and most other control properties are not marked as `virtual` and would not be able to be overridden, you would have to use the `new` keyword which can still be done with `DependencyProperties` – sa_ddam213 Sep 13 '13 at 06:37
  • You're focusing on a hypothetical, non-WPF TextBox class I invented solely to illustrate the non-dependency-property way of how you would do things. If it helps, pick *any* non-dependency-object class you want that has members marked as virtual and use them for this example. My question is how can you do something similar with Dependency Properties. And I'm also going to call you on that 'new on dependency properties' comment. You can do that on the CLR getter/setter but they are convenience methods and bindings bypass them so that wouldn't actually work except when calling from code. – Mark A. Donohoe Sep 13 '13 at 08:30

0 Answers0