5 Comments

  • The biggest problem I see with this is that you no longer have access to the DP itself. That removes a lot of the functionality for which DPs were created to begin with.

  • eran, you mean in design time when writing xaml?

  • "eran, you mean in design time when writing xaml?"

    Eran? Were you asking this about my response?

    In WPF you use the DP for several things. One, you can borrow a DP from another class without having to inherit from that class, which you won't be able to do here. You can use the DP to modify the metadata, for things like listening to the property changes externally. This solution won't work for attached DPs. I'm sure there's other details I'm missing.

    Maybe a variation would be better:

    public class CustomControl
    {
    public static DependencyProperty TextProperty = DependencyPropertyHelper.Register(typeof(CustomControl), "Text");

    [DefaultValue("hello")]
    public string Text
    {
    get { return (string)GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }
    }

    private static void OnTextChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
    }
    }

    This doesn't require a base class, still exposes the DP, and uses convention based registration. It's lacking a few details, such as being able to specify PropertyMetadata (something that's anemic in Silverlight, but important in WPF), and I'm uncertain that the sugar is all that important, but it's an interesting idea none-the-less.

  • He means you don't have access to the static property (CustomControl.TextProperty). You would need that for attached properties and for some other scenarios. Still, you should be able to have CustomControlBase.GetProperty("Text") as a static method that does it for you.

  • Hi Roy,

    This code won't work under the context of the Silverlight CLR.

    You're using private reflection to get it to work, and private reflection is not allowed in the Silverlight CLR.

    And as one of the previous posters noted, this does mean losing on of the biggest gains in C# DPs - the ability to use property references.

    That said, it's a pretty cool idea.
    I'd be more in favour of DP pre-compile generation rather than runtime-instantiation, but either way - this is a very solid step in the right direction.

    -- Justin Angel

Comments have been disabled for this content.