wpf - Binding issue on CustomControl -
i have written customcontrol on wpf contentcontrol contentpresenter , button.
here control template (located in themes/generic.xaml) :
<controltemplate targettype="{x:type local:testcontrol}"> <stackpanel background="{templatebinding background}"> <contentpresenter content="{templatebinding content}" /> <button content="next" isenabled="{templatebinding isnextbuttonenabled}" /> </stackpanel> </controltemplate>
here control code (testcontrol.cs) :
public class testcontrol : contentcontrol { /// <summary> /// identifies isnextbuttonenabled dependency property /// </summary> public readonly static dependencyproperty isnextbuttonenabledproperty = dependencyproperty.register( "isnextbuttonenabled", typeof(bool), typeof(testcontrol), new propertymetadata(true) ); static testcontrol() { defaultstylekeyproperty.overridemetadata(typeof(testcontrol), new frameworkpropertymetadata(typeof(testcontrol))); } /// <summary> /// gets or sets value indicating if next button of control enabled /// </summary> [bindableattribute(true)] public bool isnextbuttonenabled { { return (bool)getvalue(isnextbuttonenabledproperty); } set { setvalue(isnextbuttonenabledproperty, value); } } }
i want bind value of isnextbuttonenabled of testcontrol control (for example checkbox) inside content of testcontrol itself.
so did , works (when check or uncheck checkbox, button enables or disables correctly):
mainwindow.xaml
<window x:class="wpfapplication2.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:wpfapplication2" title="mainwindow" height="350" width="525"> <grid> <local:testcontrol isnextbuttonenabled="{binding elementname=cb, path=ischecked}"> <checkbox name="cb" ischecked="true" /> </local:testcontrol> </grid> </window>
but want declare testcontrol in separated xaml file. did doesn't work:
mainwindow.xaml
<window x:class="wpfapplication2.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:wpfapplication2" title="mainwindow" height="350" width="525"> <stackpanel> <local:mytestcontrol /> </stackpanel> </window>
mytestcontrol.xaml (mytestcontrol.cs not changed) :
<local:testcontrol x:class="wpfapplication2.mytestcontrol" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:wpfapplication2" isnextbuttonenabled="{binding elementname=cb, path=ischecked}"> <grid> <checkbox name="cb" ischecked="true" /> </grid> </local:testcontrol>
while executing, in output window error message :
system.windows.data error: 4 : cannot find source binding reference 'elementname=cb'. bindingexpression:path=ischecked; dataitem=null; target element 'mytestcontrol' (name=''); target property 'isnextbuttonenabled' (type 'boolean')
i don't understant mistake. did make wrong ?
thanks in advance.
try this, bind ischecked
of checkbox isnextbuttonenabled
property
<contentcontrol x:class="wpfstackoverflowspielwiese.testcontrol" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:name="control"> <grid> <checkbox name="cb" ischecked="{binding elementname=control, path=isnextbuttonenabled}" /> </grid> </contentcontrol>
code behind
public partial class testcontrol : contentcontrol { public testcontrol() { this.initializecomponent(); } /// <summary> /// identifies isnextbuttonenabled dependency property /// </summary> public static readonly dependencyproperty isnextbuttonenabledproperty = dependencyproperty.register( "isnextbuttonenabled", typeof(bool), typeof(testcontrol), new propertymetadata(true)); static testcontrol() { defaultstylekeyproperty.overridemetadata(typeof(testcontrol), new frameworkpropertymetadata(typeof(testcontrol))); } /// <summary> /// gets or sets value indicating if next button of control enabled /// </summary> [bindable(true)] public bool isnextbuttonenabled { { return (bool)this.getvalue(isnextbuttonenabledproperty); } set { this.setvalue(isnextbuttonenabledproperty, value); } } }
hope helps
Comments
Post a Comment