Silverlikes learning notes-controls Silverlight control styles

Source: Internet
Author: User

1. inline mode-that is, you can directly use its attributes inside the control to set

 

Code
< Usercontrol X: Class = " Removetextboxborder. Page "
Xmlns = " Http://schemas.microsoft.com/winfx/2006/xaml/presentation "  
Xmlns: x = " Http://schemas.microsoft.com/winfx/2006/xaml "  
Width = " 400 " Height = " 300 " >
< Grid X: Name = " Layoutroot " Background = " White " >
< Button content = " Button " Height = " 75 " Width = " 125 " Borderbrush = " Green " Foreground = " Blue "   />
</ Grid >
</ Usercontrol >

2. Global mode-defined in the app. XAML Resources file
When you create a Silverlight project in vs2008, you will get a file named "app. XAML" in the following format:

 

Code
  < Application xmlns = " Http://schemas.microsoft.com/winfx/2006/xaml/presentation "
Xmlns: x = " Http://schemas.microsoft.com/winfx/2006/xaml "  
X: Class = " Removetextboxborder. app "
>
< Application. Resources >
</ Application. Resources >
</ Application >

In this file, you can define your own styles in the following format:

 

 

  < Style X: Key = " Style name " Targettype = " Control type for the style " >  
< Setter Property = " Control property name " Value = " Control property value "   />
  </ Style >

In this example, we add the following style definitions to this file. The content of the added app. XAML file is as follows:

 

Code
< Application xmlns = " Http://schemas.microsoft.com/winfx/2006/xaml/presentation "
Xmlns: x = " Http://schemas.microsoft.com/winfx/2006/xaml "  
X: Class = " Removetextboxborder. app "
>
< Application. Resources >
< Style X: Key = " Buttonstyleone " Targettype = " Button " >
< Setter Property = " Borderbrush " Value = " Green "   />
< Setter Property = " Foreground " Value = " Blue "   />
< Setter Property = " Borderthickness " Value = " 4,4, 4,4 "   />
</ Style >
< Style X: Key = " Buttonstyletwo " Targettype = " Button " >
< Setter Property = " Borderbrush " Value = " Blue "   />
< Setter Property = " Foreground " Value = " Green "   />
</ Style >
</ Application. Resources >
</ Application >

Then we reference the defined style in the control's XAML file.

 

Code
< Usercontrol X: Class = " Removetextboxborder. Page "
Xmlns = " Http://schemas.microsoft.com/winfx/2006/xaml/presentation "  
Xmlns: x = " Http://schemas.microsoft.com/winfx/2006/xaml "  
Width = " 400 " Height = " 300 " >
< Grid X: Name = " Layoutroot " Background = " White " >
< Button content = " Button " Height = " 75 " Width = " 125 " Style = " {Staticresource buttonstyleone} " />
</ Grid >
</ Usercontrol >

 

3. in C #CodeDynamically set widget styles
To reference this control in code, we must name this control. Here we name it "mybutton"

 

< Button X: Name = " Mybutton " Content = " Button " Height = " 75 " Width = " 125 "   />

Then add the following code to the page. XAML code-behind file in its constructor:
Mybutton. Style = application. Current. Resources ["buttonstyle"] as style;
The background code is as follows:

Code
Public   Partial   Class Page: usercontrol
{
Public Page ()
{
Initializecomponent ();
Mybutton. Style = Application. Current. Resources [ " Buttonstyleone " ] As Style;
}
}

 

Silverlight learning Notes List

 

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.