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