Dependency property are static in nature, so if i create a bool type custom dependency property called as "IsGrayProperty", and implement it on two buttons. Then if I set the value in btn1 , why it should not be reflected in btn2, as the dependencyproperty is static and their is a .net wrapper property "IsGray"
-
Because btn2 is not referencing btn1's attribute. – Adam Jul 12 '16 at 14:41
2 Answers
The static readonly DependencyProperty
named IsGrayProperty
is an identifier -- a key value, so to speak. It also contains metadata about the property. It is not the actual property itself. Each DependencyObject
instance stores its own DependencyProperty
values internally.
Dependency properties are semantically very much like regular instance properties, but they have additional features which let them do things that regular instance properties can't (for example, you can change their metadata at runtime, unlike Attributes
on regular properties, and you can create attached properties). That's why they exist.
For example, TextBlock.Text
is a dependency property.
//
// Summary:
// Identifies the System.Windows.Controls.TextBlock.Text dependency property.
//
// Returns:
// The identifier of the System.Windows.Controls.TextBlock.Text dependency property.
[CommonDependencyProperty]
public static readonly DependencyProperty TextProperty;
And it's got a "regular property" wrapper, too:
//
// Summary:
// Gets or sets the text contents of a System.Windows.Controls.TextBlock.
//
// Returns:
// The text contents of this System.Windows.Controls.TextBlock. Note that all
// non-text content is stripped out, resulting in a plain text representation
// of the System.Windows.Controls.TextBlock contents. The default is System.String.Empty.
[Localizability(LocalizationCategory.Text)]
public string Text { get; set; }
When you set the Text
of one TextBlock
, does every other TextBlock
share the same text? No, of course not. And if they did, dependency properties would be totally useless.
The DependencyProperty
parameter to GetValue
tells the DependencyObject
which value to retrieve from the dependency object's own internal store of property values; if the value were stored in the DependencyProperty
itself, you would be querying the DependencyProperty
, not the DependencyObject
.
public void ForExample(TextBlock tb)
{
var oldtext = tb.GetValue(TextBlock.TextProperty) as String;
tb.SetValue(TextBlock.TextProperty, "Test value");
}
If there is only one global value for TextBlock.TextProperty
, and it's stored in TextBlock.TextProperty
, why on earth are we calling GetValue()
and SetValue()
on some random instance of TextBlock
? Why get a particular TextBlock
instance into the act at all?
Instead, we would be calling TextBlock.TextProperty.SetValue()
.
It's just like a Dictionary:
var d1 = new Dictionary<String, Object>();
var d2 = new Dictionary<String, Object>();
var keyValue = "Foo";
d1.Add(keyValue, 32);
Console.WriteLine( d2[keyValue] );
What would you expect from d2[keyValue]
? You would expect nothing, because you never gave d2
a value for keyValue
. d1.Add("Foo",32)
does not store the integer 32
in the string "Foo"
. It stores it in the dictionary.
Each dictionary stores its own key values. Each DependencyObject
stores its own property values. Internally, it probably stores them in a Dictionary
; why not? If the .NET team could write a more efficient way to store key/value pairs than Dictionary
, they'd have called it Dictionary
.
When you have an idea about what a language or framework feature means, always ask yourself, "If my idea were true, would the feature be totally useless or massively harmful?" If the answer to that question is "yes", then you guessed wrong about what the feature means. That's 100% guaranteed, because language and framework designers and implementers don't habitually waste months of effort designing features which are totally useless (classes in Perl 5 come close, though).

- 36,786
- 7
- 62
- 114
-
Thanks Ed, yeah you are right, but didn't get how an static property can have different values for different controls, though GetValue, Setvalue at the end accessing the static readonly property – Raj Jul 12 '16 at 15:02
-
It's exactly equivalent to retrieving items from a dictionary: `var foo = new Dictionary
(); /* add stuff */ var x = foo["Bar"];`. If you have six different dictionaries, each stores its own personal value for "Bar". The `DependencyProperty` parameter to `GetValue` tells the `DependencyObject` which value to retrieve from its own internal store; if the value were stored in the `DependencyProperty` itself, you would be querying the `DependencyProperty`, *not* the `DependencyObject`. – 15ee8f99-57ff-4f92-890c-b56153 Jul 12 '16 at 15:05
What is really static, is dependency property descriptor:
public static readonly DependencyProperty IsGrayProperty =
DependencyProperty.Register(
"IsGray",
typeof(bool),
typeof(MyButton));
The descriptor only contains metadata about the property, and it doesn't contain its value.
The actual property that is used with data binding and accessed from code, is the instance one:
public bool IsGray
{
get
{
return (bool)GetValue(IsGrayProperty);
}
set
{
SetValue(IsGrayProperty, value);
}
}
as you can see, it delegates get and set to instance methods GetValue
and SetValue
of base DependencyObject
.
Internally, DependencyObject
maintains an array of entries (in an instance field), where the actual values of its dependency properties are stored. Each instance has its own array, and the actual value resides in one of the entries. As such, it is never shared with other instances. Thus, SetValue
alters the state of the current button instance, rather than any static state.
The static IsGrayProperty
descriptor is only used to determine an index into the entry array, perform validations, etc. The fact that IsGrayProperty
is static, makes it somewhat anti-intuitive to think about, but nevertheless, dependency property values are per instance of dependency object, and are not shared.

- 8,178
- 1
- 26
- 36
-
Thanks Felix,By using the GetValue and SetValue, i am accessing the registered static IsGrayproperty, in that case the static property will have only one value at a time, so that should be same for all the control implemented that, but that doesn't happen, I want to know how does dependency property actually works – Raj Jul 12 '16 at 14:59
-
No, no, no. By using GetValue and SetValue, you access an instance field that holds array of entries, private to every instance. The IsGrayProperty descriptor is only used to determine an index into the array, perform validations, etc. The actual value read and written by GetValue and SetValue, is at entry in the entry array, and each instance has its own entry array. The fact that IsGrayProperty is static, makes it somewhat anti-intuitive to think about it, but nevertheless, dependency property values are per instance, and are not shared. – felix-b Jul 12 '16 at 15:06