wpf - Usercontrol within Usercontrol with Data Binding -
i built simple wpf usercontrol want implement several of in usercontrol while maintaining data binding. have simple debug1 window implements (1) bound textblock (to verify data binding only) & button; (2) single instance of numericupdownbox usercontrol binding; , (3) instance of parent user control containing instance of child user control numericupdownbox.
items (1) , (2) working. item (3) not. feel missing in implementation in parent user control code? couple of errors concern me (see below).
debug1 window xaml:
<window x:class="debug1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:cfit" mc:ignorable="d" title="debug1" height="225" width="600" closing="closingdebug1"> <grid> <stackpanel grid.row="0" orientation="horizontal"> <button name="nextbutton" content="next" click="clicknext" width="50" /> <textbox text="{binding path=years, mode=twoway}" /> </stackpanel> <local:numericupdownbox grid.row="1" x:name="singlebox" label="tbd" increment="2" value="{binding path=years}"/> <local:ymdh_timecontrol grid.row="2" x:name="timecontrol" yearsvalue="{binding path=years}" /> </grid> </window>
debug window vb code:
public class debug1 public mat new clsmaterial sub new() ' removed brevity end sub private sub clicknext(sender object, e routedeventargs) mat.lives(0).msdateoffset.years = mat.lives(0).msdateoffset.years + 5 end sub end class
parent usercontrol xaml:
<usercontrol x:class="ymdh_timecontrol" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:cfit" mc:ignorable="d" d:designheight="50" d:designwidth="200" minheight="30" minwidth="160" name="ymdh_timecontrol"> <grid datacontext="{binding elementname=ymdh_timecontrol}"> <local:numericupdownbox grid.column="0" x:name="years"/> </grid> </usercontrol>
parent usercontrol vb code:
public class ymdh_timecontrol public shared readonly yearsvalueproperty dependencyproperty = dependencyproperty.register("yearsvalue", gettype(integer), gettype(ymdh_timecontrol), new frameworkpropertymetadata(0, (frameworkpropertymetadataoptions.bindstwowaybydefault), nothing, new coercevaluecallback(addressof onvaluechanged))) private shared function onvaluechanged(d dependencyobject, value object) object if isnumeric(value) return cint(value) else return nothing end if end function public property yearsvalue integer return years.value end set(value integer) years.value = value end set end property end class
child usercontrol xaml:
<usercontrol x:class="numericupdownbox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:cfit" mc:ignorable="d" d:designheight="50" d:designwidth="50" minheight="30" minwidth="40" name="numericupdownbox"> <grid datacontext="{binding elementname=numericupdownbox}"> <grid.resources> ... removed brevity... </grid.resources> <!--box definition--> <border style="{staticresource sectionnameborder}" grid.column="0"> <grid background="gray" > <viewbox> <textblock name="labeltextblock" text="{binding path=label, elementname=numericupdownbox}" verticalalignment="center" horizontalalignment="center"/> </viewbox> <grid grid.row="2" background="white" > <grid grid.column="0" horizontalalignment="right"> <viewbox> <textbox name="valuetextbox" borderbrush="white" text="{binding path=value, mode=twoway, elementname=numericupdownbox, updatesourcetrigger=propertychanged}" /> </viewbox> </grid> <grid grid.column="1"> <repeatbutton name="uparrow" style="{staticresource uparrowstyle}" tag="+" click="tickvalues" /> <repeatbutton name="downarrow" style="{staticresource downarrowstyle}" tag="-" click="tickvalues" /> </grid> </grid> </grid> </border> </grid> </usercontrol>
child usercontrol vb code:
public class numericupdownbox private const defaultinc integer = 1 public sub new() initializecomponent() if increment = 0 increment = defaultinc end sub private sub tickvalues(sender object, e routedeventargs) dim sign char dim tag string tag = sender.tag.tostring if len(tag) = 0 exit sub sign = left(tag, 1) if sign = "+" value = value + increment elseif sign = "-" value = value - increment end if end sub public property value integer return cint(getvalue(valueproperty)) end set(value integer) if value < 0 value = 0 setvalue(valueproperty, value) end set end property public property label string return getvalue(labelproperty) end set(value string) setvalue(labelproperty, value) end set end property public property increment integer return cint(getvalue(incproperty)) end set(value integer) if value < 0 value = 0 setvalue(incproperty, value) end set end property public shared readonly valueproperty dependencyproperty = dependencyproperty.register("value", gettype(integer), gettype(numericupdownbox), new frameworkpropertymetadata(0, (frameworkpropertymetadataoptions.bindstwowaybydefault), nothing, new coercevaluecallback(addressof onvaluechanged))) public shared readonly labelproperty dependencyproperty = dependencyproperty.register("label", gettype(string), gettype(numericupdownbox), new frameworkpropertymetadata("")) public shared readonly incproperty dependencyproperty = dependencyproperty.register("increment", gettype(integer), gettype(numericupdownbox), new frameworkpropertymetadata(0, nothing, new coercevaluecallback(addressof onvaluechanged))) private shared function onvaluechanged(d dependencyobject, value object) object if isnumeric(value) return cint(value) else return nothing end if end function end class
example information error in output window:
system.windows.data information: 10 : cannot retrieve value using binding , no valid fallback value exists; using default instead. bindingexpression:path=years; dataitem=null; target element 'ymdh_timecontrol' (name='timecontrol'); target property 'yearsvalue' (type 'int32')
how data binding pass through parent user control?
Comments
Post a Comment