wpf - XAML Dependency Property vs Regular Properties -
if have this:
public class boardcalc : frameworkelement { public boardcalc() { this.loaded += boardcalc_loaded; } void boardcalc_loaded(object sender, routedeventargs e) { boards = math.floor(lengthrequired / 16); boardsrequired2 = math.floor(lengthrequired / 16); } public double lengthrequired { get; set; } private double _boards; public double boards { { return _boards; } set { _boards = value; } } //public double boards //{ // { return (double)getvalue(boardsproperty); } // set { setvalue(boardsproperty, value); } //} //// using dependencyproperty backing store boards. enables animation, styling, binding, etc... //public static readonly dependencyproperty boardsproperty = // dependencyproperty.register("boards", typeof(double), typeof(boardcalc), null); public double boardsrequired2 { { return (double)getvalue(boardsrequired2property); } set { setvalue(boardsrequired2property, value); } } // using dependencyproperty backing store boardsrequired2. enables animation, styling, binding, etc... public static readonly dependencyproperty boardsrequired2property = dependencyproperty.register("boardsrequired2", typeof(double), typeof(boardcalc), null); }
and this:
<stackpanel xmlns:boards="clr-namespace:boardsutil" > <boards:boardcalc x:name="boardcalc1" lengthrequired="5280" /> <textblock text="{binding elementname=boardcalc1, path=boards}" /> <textblock text="{binding elementname=boardcalc1, path=boardsrequired2}" /> </stackpanel>
two part question:
when use dependency property, boards value calculated @ in designer , show 330 boards. if use regular property 0 @ design time. @ runtime, either 1 works. expect? if so, or if not, can explain me why is, can work around , check see if rest of code working.
should using dependency property lengthrequired? if set property xaml, should use dependency yes? if merely bind property xaml can use regular property? behavior see here? expect? no? yes? why, can decide do.
the main reason use dependency properties allow underlying subsystem provide additional ui/xaml/wpf based functionality, namely:
1) binding. in code:
<slider x:name="slid1" maximum="5280" minimum="16" value="250" /> <boards:boardcalc x:name="boardcalc1" lengthrequired="{binding elementname=slid1,path=value" />
lengthrequired must dependency property. can set lengthrequired
lengthrequired = "5280"
and can this
text={binding elementname=boardcalc1, path=lengthrequired} ...
but can't set lengthrequired using binding extension.
2) animation 3) styling
same basic principle. allow underlying ui subsystem animate 0 100 or whatever, or subsystem pick styling , themes , whatever, must dependency property.
1,2,3. reasons use dependency property. regular properties can jam in inotify.
Comments
Post a Comment