Share via


Sometimes you just gotta do the best you can [Tip: Read-only custom DependencyProperties don't exist in Silverlight, but can be closely approximated]

**

This blog has moved to a new location and comments have been disabled.

All old posts, new posts, and comments can be found on The blog of dlaa.me.

See you there!

Comments

  • Anonymous
    March 30, 2010
    The comment has been removed

  • Anonymous
    March 30, 2010
    Mike Strobel, Sorry about the frustration! For what it's worth, the Silverlight team tends to implement features based on how necessary they are for the current customer segment. In cases like this one where it's possible to work around a missing feature (or very nearly so), it's sometimes the case that some different feature gets prioritized higher because what that other feature enables is simply impossible without platform support. Therefore, while I miss read-only DependencyProperties, too, I wouldn't read too far into the fact that they haven't made it into Silverlight quite yet. :) Thanks for your comment!

  • Anonymous
    March 30, 2010
    The comment has been removed

  • Anonymous
    March 31, 2010
    DrWPF, Wow, that is a great suggestion! I'm emailing you now to discuss a couple of things about it, but once that's settled, it sounds like I need another DependencyProperty tip to discuss your solution. Thanks very much!

  • Anonymous
    March 31, 2010
    There's a MUCH easier way: [assembly: InternalsVisibleTo("System.Windows, PublicKey=00240000048000009400000006020000002400005253413100040000010001008d56c76f9e8649383049f383c44be0ec204181822a6c31cf5eb7ef486944d032188ea1d3920763712ccb12d75fb77e9811149e6148e5d32fbaab37611c1878ddc19e20ef135d0cb2cff2bfec3d115810c3d9069638fe4be215dbf795861920e5ab6f7db2e2ceef136ac23d5dd2bf031700aec232f6c6b1c785b4305c123b37ab")] public int MyReadOnly {    get { return (int)GetValue(MyReadOnlyProperty); }    private set    {           SetValue(MyReadOnlyProperty, value);    } } internal static readonly DependencyProperty MyReadOnlyProperty = DependencyProperty.Register(    "MyReadOnly", typeof(int), typeof(MyControl), new PropertyMetadata(0));

  • Anonymous
    March 31, 2010
    btw are you now saying that it's "illegal" to do this: myObject.SetValue(MyReadOnlyProperty,5) ?

  • Anonymous
    March 31, 2010
    SharpGIS, I'm saying that WPF will not allow "SetValue(MyReadOnlyProperty, 5)" to succeed on a read-only DependencyProperty (you need to use the private DependencyPropertyKey instead) and therefore that the same code should not work for "simulated read-only" properties on Silverlight, either. Regarding your InternalsVisibleTo suggestion, I've Twittered you just now to discuss this further. I'll post a follow-up comment here after we've come to an agreement. :)

  • Anonymous
    March 31, 2010
    SharpGIS, Your InternalsVisibleTo solution excites and revolts me - at the same time. :) I'll be doing a follow-up blog post to this one and I'll include your suggestion along with DrWPF's. Thanks very much for sharing!

  • Anonymous
    April 01, 2010
    The comment has been removed

  • Anonymous
    November 12, 2010
    Wouldn't that be a problem if you have several read-only properties in the same class? You might want to set other properties when one of them changes, so I would add _changingMyReadOnly = false; right before myControl.OnMyReadOnlyChanged((int)e.OldValue, (int)e.NewValue);

  • Anonymous
    November 12, 2010
    jariza, That's a great point, thanks for bringing it up! On the other hand, you might consider having a dedicated instance of the _changingMyReadOnly/_restoringMyReadOnly variables for each read-only DependencyProperty in a class so they're all truly independent. Without analyzing it too much, I feel like this latter approach might be a bit easier to get right and more straightforward to think about.

  • Anonymous
    April 22, 2011
    The comment has been removed

  • Anonymous
    January 24, 2012
    Won't work on attached DPs though