Inherit contentcontrol to easily implement controls containing content. solution 1:
Custom Controls Code As follows:
Public Class Dockpanel: contentcontrol
{
Public Dockpanel ()
{
This . Defaultstylekey = Typeof (Dockpanel );
}
} The code for themes/generic. XAML is as follows:
< Style Targettype = "Local: dockpanel" >
< Setter Property = "Template" >
< Setter. Value >
< Controltemplate Targettype = "Local: dockpanel" >
< Grid >
< Border >
< Grid >
< Contentpresenter Content =" {Templatebinding content} " />
</ Grid >
</ Border >
</ Grid >
</ Controltemplate >
</ Setter. Value >
</ Setter >
</ Style > Reference Page code:
< Usercontrol X: Class = "Silverlightapplication1.page"
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: cinlap = "CLR-namespace: silverlightclasslibrary1; Assembly = silverlightclasslibrary1"
Width = "400" Height = "300" >
< Grid X: Name = "Layoutroot" Background = "Yellow" Width = "400" Height = "300" >
< Cinlap: dockpanel >
< Cinlap: dockpanel. Content >
< Stackpanel Background = "Red" />
</ Cinlap: dockpanel. Content >
</ Cinlap: dockpanel >
</ Grid >
</ Usercontrol >
Solution 2:
Custom Controls: Public Class Mycontentcontrol: Control
{
Public Static Readonly Dependencyproperty contentpanelproperty =
Dependencyproperty. Register ( " Contentpanel " , Typeof (Panel ), Typeof (Mycontentcontrol ), Null );
PublicMycontentcontrol ()
{
This. Defaultstylekey= Typeof(Mycontentcontrol );
}
Public Panel contentpanel
{
Get
{
Return (Panel) This . Getvalue (contentpanelproperty );
}
Set
{
This . Setvalue (contentpanelproperty, value );
}
}
} The code for themes/generic. XAML is as follows: < Resourcedictionary Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: Local = "CLR-namespace: silverlightclasslibrary1" >
< Style Targettype = "Local: mycontentcontrol" >
< Setter Property = "Template" >
< Setter. Value >
< Controltemplate Targettype = "Local: mycontentcontrol" >
< Grid >
< Border >
< Grid >
< Contentpresenter Content =" {Templatebinding contentpanel} " />
</ Grid >
</ Border >
</ Grid >
</ Controltemplate >
</ Setter. Value >
</ Setter >
</ Style >
</ Resourcedictionary > Reference Page code: < Grid X: Name = "Layoutroot" Background = "Yellow" Width = "400" Height = "300" >
< Cinlap: mycontentcontrol >
< Cinlap: mycontentcontrol. contentpanel >
< Stackpanel Background = "Red" />
</ Cinlap: mycontentcontrol. contentpanel >
</ Cinlap: mycontentcontrol >
</ Grid > Supplement: In the content control provided by Silverlight, the following attributes are bound to contentpresenter:
Horizontalalignment = "{templatebinding horizontalcontentalignment }"
Verticalalignment = "{templatebinding verticalcontentalignment }"
After the experiment, after defining these two attributes, the content elements cannot be scaled automatically. Therefore, when customizing the content control, you can use these two attributes as needed.