c# - Inherit DependencyProperties -


i have created several usercontrols headereddatepicker.

the xaml of usercontrol looks like:

<usercontrol x:class="book.customcontrols.headereddatepicker"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              datacontext="{binding relativesource={relativesource self}}"              width="200" height="50">     <grid>         <grid.rowdefinitions>             <rowdefinition height="auto"/>             <rowdefinition/>         </grid.rowdefinitions>         <textblock grid.row="0" margin="2,2" verticalalignment="center" text="{binding header}" fontweight="{binding headerfontweight}"/>         <datepicker grid.row="1" margin="2,2" verticalalignment="center" selecteddate="{binding date, mode=twoway, updatesourcetrigger=propertychanged}"/>     </grid> </usercontrol> 

the code-behind registered dependencypropertys is:

public partial class headereddatepicker : usercontrol {     public headereddatepicker()     {         this.initializecomponent();         this.headerfontweight = fontweights.normal;         this.date = datetime.now;     }      public static readonly dependencyproperty headerproperty = dependencyproperty.register("header", typeof(string), typeof(headereddatepicker));     public static readonly dependencyproperty headerfontweightproperty = dependencyproperty.register("headerfontweight", typeof(fontweight), typeof(headereddatepicker));     public static readonly dependencyproperty dateproperty = dependencyproperty.register("date", typeof(datetime), typeof(headereddatepicker));      public string header     {         { return (string)getvalue(headerproperty); }         set { setvalue(headerproperty, value); }     }      public fontweight headerfontweight     {         { return (fontweight)getvalue(headerfontweightproperty); }         set { setvalue(headerfontweightproperty, value); }     }      public datetime date     {         { return (datetime)getvalue(dateproperty); }         set { setvalue(dateproperty, value); }     } } 

everything working fine. want create headeredcontrol headeredcombobox or so. question is:

do have write headerproperty , headerfontweightproperty in each code-behind-file or there way in base-class?

i tried create base-class properties registered, in code-behind of headereddatepicker couldn't inherit class

public class main : usercontrol {     public static readonly dependencyproperty headerproperty = dependencyproperty.register("header", typeof(string), typeof(main));     public static readonly dependencyproperty headerfontweightproperty = dependencyproperty.register("headerfontweight", typeof(fontweight), typeof(main));     public static readonly dependencyproperty dateproperty = dependencyproperty.register("date", typeof(datetime), typeof(main));      public string header     {         { return (string)getvalue(headerproperty); }         set { setvalue(headerproperty, value); }     }      public fontweight headerfontweight     {         { return (fontweight)getvalue(headerfontweightproperty); }         set { setvalue(headerfontweightproperty, value); }     }      public datetime date     {         { return (datetime)getvalue(dateproperty); }         set { setvalue(dateproperty, value); }     }  } 

then in every usercontrol supposed derive declared main.

    public partial class headeredcombobox : main {     public headeredcombobox()     {         this.initializecomponent();         this.datacontext = this;     }  } 

finally

<local:main x:class="wpfapplication5.headeredcombobox"          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:wpfapplication5"          mc:ignorable="d"           width="200" height="50"> <grid>     <grid.rowdefinitions>         <rowdefinition height="auto"/>         <rowdefinition/>     </grid.rowdefinitions>     <textblock grid.row="0" margin="2,2" verticalalignment="center" text="{binding header}" fontweight="{binding headerfontweight}"/>     <combobox grid.row="1" margin="2,2" verticalalignment="center" itemssource="{binding itemssource, updatesourcetrigger=propertychanged}"               selecteditem="{binding selecteditem, mode=twoway, updatesourcetrigger=propertychanged}"/> </grid> 

let me know whether works.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -