Thursday, October 8, 2015

WPF: Styling a button in code.


Add Style in your App.xaml file (placed on the root of your project).

   <Application.Resources>
<Style x:Key="MyButton" TargetType="Button">
            <Setter Property="Background" Value="Red" />
            <Setter Property="Width" Value="100"></Setter>
            <Setter Property="Height" Value="100"></Setter>
 </Style>
   <Application.Resources>


In .CS file. 

  Button btn = new Button();
  Style style = this.FindResource("MyButton") as Style;
  btn.Style = style

No comments:

Post a Comment