Controls are generally assigned a value on the XAML page. However, in some special environments, such as dynamically generating controls, you need to add styles to the controls on the CS page.
Method 1:
Add the style as a global resource on the app. XAML page.
<Application. Resources>
......
</Application. Resources>
Then, the other CS pages go through the followingCodeTo assign a style
Style mystyle = application. Current. Resources [stylename] as style;
Button bt = new button ();
Bt. Style = mystyle;
Method 2:
Write style code directly on the CS page
For example:
VaR style = new style ();
Style. setters. Add (New setter (backgroundproperty, new solidcolorbrush (colors. Red )));
Style. setters. Add (New setter (foregroundproperty, new solidcolorbrush (colors. Blue )));
VaR stylecopy = new style ();
foreach (VAR Setter in style. setters)
{< br> var typedsetter = setter as setter;
If (typedsetter! = NULL)
{< br> var newsetter = new setter (typedsetter. property, typedsetter. value);
stylecopy. setters. add (newsetter);
}< BR >}