Saturday, August 24, 2013

AttachedProperty - first lesson

AProp is one of the sophisticated features in WPF IMHO. First get a grip on DependencyProp (DProp) -- see other blog posts.

--motivation for AProp--
If you want a concrete justification for AProp, look no further than Dock. Many top top WPF experts pick Dock as the showcase of AProp. Don't read about StackPanel or FontSize. Don't both with inheritance - we will deal with them much later.

Focus on #1 usage. Don't bother with the #2 or #3, such as the "inheritance" or the "service" patterns in

Spend enough time and go deep enough to understand the Dock usage, before digging deeper.

Here's the Dock usage outlined on MSDN. "The DockPanel.Dock property is created as an attached property because it is designed to be set on child controls within a DockPanel, rather than on DockPanel itself. The DockPanel class defines the static AProp named DockProperty, and then provides the GetDock and SetDock *static* accessors for the AProp. "

--storage of the AProp values--
Same as ordinary DProp. One hash table per child DependencyObject. Absolutely Irrelevant is the hash table of the dock panel.

[[Pro WPF & Silverlight MVVM]] says "Once a DependencyProperty is registered as an attached property using the DependencyProperty.RegisterAttached method, that property can be set on any DependencyObject subclass."

--how to set/get--
In xaml, the aprop "Dock" looks comfortably like any other property of the child (eg Button), but we know that DockPanel not Button defines this property. Therefore the setter has to be defined in DockPanel. A Xaml declaration would translate to

   DockPanel.SetDock(myButton, Dock.Left); //STATIC method call

Internally, this runs

  buttonArgument.SetValue(DockPanel.DockProperty, Dock.Left); //AP setter

Look at this AP Setter 3 times, 5 times until you clearly understands --

* SetValue() is a NON-static method defined in the base class DependencyObject. Internally this method uses that hash table thing. All DProp and AProp setters goes through this Choke point.
* Note the dock property is unrelated to the host object i.e. the button. This is the funny/magical thing about Attached Property.
* Physically, the button object's (NOT the panel's) hash table records key={theButton.type +}, value=Left. This property is useful to the parent container (the doc panel) during layout. The dock panel will scan all children, querying this button's dock property, and another button's doc property ...

MSDN said "Attached properties are a XAML concept, whereas dependency properties are a WPF concept". The way you set a AProp is very natural in xaml but not in code.

--extreme freedom--
The top top experts like to emphasize the extreme flexibility to attach anything to anything, whether meaningful or not.

Bencon said something like "simplest way to explain an attached property is that it allows the place where the property is defined and the place where it is stored to be completely different classes that know nothing about each other. Property value can only be stored on DependencyObject objects (which happens to be the base class for most Avalon things)."

Nick said things like

"You can call panel.SetValue(CheckBox.CheckedProperty, true), and property engine will happily set that attached property to true. Doesn't matter that nobody is ever going to try to read that property value. Doesn't matter that the author of CheckBox probably never intended this."

"From a property engine perspective, it's irrelevant that Button happens to be inside a DockPanel. You could set DockPanel.Dock even if your parent is a Grid -- that property won't do any good, because Grid doesn't look at the DockPanel.Dock property, but you can do it."

Bencon -
Nick --

No comments:

Total Pageviews

my favorite topics (labels)

_fuxi (302) _misLabel (13) _orig? (3) _rm (2) _vague (2) clarified (58) cpp (39) cpp_const (22) cpp_real (76) cpp/java/c# (101) cppBig4 (54) cppSmartPtr (35) cppSTL (33) cppSTL_itr (27) cppSTL_real (26) cppTemplate (28) creditMkt (14) db (65) db_sybase (43) deepUnder (31) dotnet (20) ECN (27) econ/bank` (36) fin/sys_misc (43) finGreek (34) finReal (45) finRisk (30) finTechDesign (46) finTechMisc (32) finVol (66) FixedIncom (28) fMath (7) fMathOption (33) fMathStoch (67) forex (39) gr8IV_Q (46) GTD_skill (15) GUI_event (30) inMemDB (42) intuit_math (41) intuitFinance (57) javaMisc (68) javaServerSide (13) lambda/delegate (22) marketData (28) math (10) mathStat (55) memIssue (8) memMgmt (66) metaProgram` (6) OO_Design (84) original_content (749) polymorphic/vptr (40) productive (21) ptr/ref (48) py (28) reflect (8) script`/unix (82) socket/stream (39) subquery/join (30) subvert (13) swing/wpf (9) sysProgram` (16) thread (164) thread_CAS (15) thread_cpp (28) Thread* (22) timeSaver (80) transactional (23) tune (24) tuneDB (40) tuneLatency (30) z_ajax (9) z_algoDataStruct (41) z_arch (26) z_arch_job (27) z_automateTest (17) z_autoTrad` (19) z_bestPractice (39) z_bold (83) z_bondMath (35) z_book (18) z_boost (19) z_byRef^Val (32) z_c#GUI (43) z_c#misc (80) z_cast/convert (28) z_container (67) z_cStr/arr (39) z_Favorite* (8) z_FIX (15) z_forex (48) z_fwd_Deal (18) z_gz=job (33) z_gzBig20 (13) z_gzMgr (13) z_gzPain (20) z_gzThreat (19) z_hib (19) z_IDE (52) z_ikm (5) z_IR_misc (36) z_IRS (26) z_javaWeb (28) z_jdbc (10) z_jobFinTech (46) z_jobHunt (20) z_jobRealXp (10) z_jobStrength (15) z_jobUS^asia (27) z_letter (42) z_linq (10) z_memberHid` (11) z_MOM (54) z_nestedClass (5) z_oq (24) z_PCP (12) z_pearl (1) z_php (20) z_prodSupport (7) z_py (31) z_quant (14) z_regex (8) z_rv (38) z_skillist (48) z_slic`Problem (6) z_SOA (14) z_spring (25) z_src_code (8) z_swingMisc (50) z_swingTable (26) z_unpublish (2) z_VBA/Excel (8) z_windoz (17) z_wpfCommand (9)

About Me

New York (Time Square), NY, United States