Thursday, April 19, 2007

Applying same style to all wpf elements of same type

The folloging code will give a Red background to all the Buttons in the Window provided if we didnt set the ButtonBackground explicitly

< Window.Resources >
< Style x:key="{x:Type Button}" TargeType="{x:Type Button}" >
< Setter Property ="Background" Value ="Red"/ >
< /Style >
< /Window.Resources >

C# Code

Style sty = new Style(typeof(Button));

Setter set = new Setter(Button.BackgroundProperty , Brushes.Red);


Resources.Add(typeof( Button), sty);