c# - Dependency Property mechanism -
this question has answer here:
- why dependency properties “static”? 2 answers
dependency property static in nature, if create bool type custom dependency property called "isgrayproperty", , implement on 2 buttons. if set value in btn1 , why should not reflected in btn2, dependencyproperty static , .net wrapper property "isgray"
the static readonly dependencyproperty
named isgrayproperty
identifier -- key value, speak. contains metadata property. not actual property itself. each dependencyobject
instance stores own dependencyproperty
values internally.
dependency properties semantically regular instance properties, have additional features let them things regular instance properties can't (for example, can change metadata @ runtime, unlike attributes
on regular properties, , can create attached properties). that's why exist.
for example, textblock.text
dependency property.
// // summary: // identifies system.windows.controls.textblock.text dependency property. // // returns: // identifier of system.windows.controls.textblock.text dependency property. [commondependencyproperty] public static readonly dependencyproperty textproperty;
and it's got "regular property" wrapper, too:
// // summary: // gets or sets text contents of system.windows.controls.textblock. // // returns: // text contents of system.windows.controls.textblock. note // non-text content stripped out, resulting in plain text representation // of system.windows.controls.textblock contents. default system.string.empty. [localizability(localizationcategory.text)] public string text { get; set; }
when set text
of 1 textblock
, every other textblock
share same text? no, of course not. , if did, dependency properties totally useless.
the dependencyproperty
parameter getvalue
tells dependencyobject
value retrieve dependency object's own internal store of property values; if value stored in dependencyproperty
itself, querying dependencyproperty
, not dependencyobject
.
public void forexample(textblock tb) { var oldtext = tb.getvalue(textblock.textproperty) string; tb.setvalue(textblock.textproperty, "test value"); }
if there 1 global value textblock.textproperty
, , it's stored in textblock.textproperty
, why on earth calling getvalue()
, setvalue()
on random instance of textblock
? why particular textblock
instance act @ all?
instead, calling textblock.textproperty.setvalue()
.
it's 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 expect d2[keyvalue]
? expect nothing, because never gave d2
value keyvalue
. d1.add("foo",32)
not store integer 32
in string "foo"
. stores in the dictionary.
each dictionary stores own key values. each dependencyobject
stores own property values. internally, stores them in dictionary
; why not? if .net team write more efficient way store key/value pairs dictionary
, they'd have called dictionary
.
when have idea language or framework feature means, ask yourself, "if idea true, feature totally useless or massively harmful?" if answer question "yes", you guessed wrong feature means. that's 100% guaranteed, because language , framework designers , implementers don't habitually waste months of effort designing features totally useless (classes in perl 5 come close, though).
Comments
Post a Comment