[Original] Silverlight two custom Content Control Schemes

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.