Where does it make sense to have AttachedProperties as private vs public?
Usually it is define as (example):
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached(
"Command",
typeof(ICommand),
typeof(Click),
new PropertyMetadata(OnSetCommandCallback));
But I have also seen examples where some properties are private static readonly...
What are the consequences if I change the above CommandProperty to private now? It seems to be still available in my XAML intellisense if I do that. What am I missing here?
The difference is that you won’t be able to access the
DependencyPropertyfrom outside of the class. This can make sense if the static Get and Set methods are private as well, (in an attached behavior where you need to store some behavior local data for example) but not otherwise (and I don’t think I’ve ever seen this with public Get and Set).An example of when you want to use the
DependencyPropertyisDependencyPropertyDescriptor. With a publicDependencyPropertyyou can do the followingBut if the
DependencyPropertyis private, the above code won’t work.However, the following will work fine for both a public and private
DependencyProperty(if the static Get and Set methods are public) since the owner class can access the privateDependencyProperty. This also goes for Bindings and values set through Xaml whereGetValueandSetValueare called directly.If you look through the framework you will notice that all of the public attached properties have a public
DependencyProperty, for exampleGrid.RowPropertyandStoryboard.TargetNameProperty. So if the attached property is public, use a publicDependencyProperty