0

In WPF there are many DependencyProperties and their CLR setters and getters marked as public. This is bad as anyone can set their Value. I wonder how I can create a private RenderTransform property that can only be set from the inside of the class?

HerpDerpington
  • 3,751
  • 4
  • 27
  • 43
  • 2
    They're public because they have to be set via XAML (where settable properties must be public). That said...it's not something bad (what's the meaning to have a property noone can change?!) and finally...what do you think you can do with a PRIVATE DEPENDENCY PROPERTY? – Adriano Repetti Sep 09 '13 at 13:46
  • 1
    @Adriano I've got a working use case with private dependency properties used within a user control here http://stackoverflow.com/questions/18332633/bindings-inside-wpf-user-control – Philippe Sep 09 '13 at 13:49
  • 1
    @Adriano readonly dependency properties are useful when you want to communicate some state to a style trigger. – jamesSampica Sep 09 '13 at 13:52
  • @Adriano the last sentence describes what I'm trying to archieve: safety. – HerpDerpington Sep 09 '13 at 13:54
  • @Philippe from that use case I don't see a _reason_ to do it but just the possibility. "Yes you can do it...here a way to do it". – Adriano Repetti Sep 09 '13 at 14:01
  • @Shoe readonly DP are OK, I'm questioning if a **private** DP (both getter and setter) is useful... – Adriano Repetti Sep 09 '13 at 14:02
  • 1
    @Adriano, sure it is not necessary to have them private, but as the property defining which UI editor will be shown within the user control is only used inside the control, I didn't want to expose the properties to the outside world. – Philippe Sep 09 '13 at 14:04
  • @user1574054 I would see a case when you need a DP (not just a _helper_ plain property) private (where both getter and setter are private). As pointed in Philippe's link it's at least unusual (it doesn't mean it's wrong but...I would consider to do not use DP in that case). – Adriano Repetti Sep 09 '13 at 14:06
  • @Philippe I understand but **I wouldn't use three DPs in that case** (and I can't imagine a case where they are necessary). I can't see added value for that (they're used, at least in the example, as plain properties). In that case (well, it's just my opinion) I would use a **DataTrigger**. IMO if you feel you need a private DP (full private, not just readonly) then you're using DP where you should not. – Adriano Repetti Sep 09 '13 at 14:14
  • @Adriano The way the question reads to me concerns readonly DPs. _private_ `RenderTransform` _property that can only be set from the inside of the class_. It says nothing about a private get – jamesSampica Sep 09 '13 at 14:22
  • And even if the getter was private: the transform itself is what has to happen. – HerpDerpington Sep 09 '13 at 14:25
  • @Shoe if getter is public then...no problem (!) even if I still wonder if it's a good design or not (it's pretty rare to really need a private DP setter, see my comment to Philippe). – Adriano Repetti Sep 09 '13 at 15:15

1 Answers1

3

Your conclusion is wrong: A depencency property being public does not mean that "anybody can set its value".

If you want a dependency property that can only be set from the inside of the class, the usual pattern is to create a private depencendy property key together with a public dependency property. A detailed example can be found in this question:

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519