0

Title says it all. Not thinking this is possible due to the way DependencyProperties work, but figured I'd ask. Can you change a read-write property to a read-only property in a subclass?

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • Couldn't you use inheritance `public class MyDependencyObject : DependencyObject{}` and then just override the field you want with a read-only version? `public override readonly string MyString` can't get to an IDE atm so not sure if this is super helpful – nulltron Jan 23 '19 at 22:28
  • Yes, but how do you override a dependency property? Remember the CLR accessors for a DP are exactly just that... accessors. They don't stop you from setting the values in other ways (e.g. SetValue, etc.) – Mark A. Donohoe Jan 23 '19 at 22:29
  • @MarquelV damn you're right. Could you do something like this? `public string MyString => base.MyString`? – nulltron Jan 23 '19 at 22:31
  • Again, that's just POCO properties, not DP properties. You register a DP property specifically as normal or as read-only. That's why I don't think you can change it. – Mark A. Donohoe Jan 23 '19 at 22:36
  • From what I've been reading I think you're right... apologies for not being useful – nulltron Jan 23 '19 at 22:42
  • No worries. They say if you have to try really hard to get something to work, chances are there's an easier way you're not considering. – Mark A. Donohoe Jan 24 '19 at 05:47
  • I don't think there is a 'common' way to do that. Accessibility is a feature of a dependency property, and is not belong to property metadata that can be override. But dependency property handle accessibility in a very simple way, if you only use subclass, you can still change is by reflecting through some pravite members. – Alex.Wei Jan 24 '19 at 06:16

0 Answers0