Apply a Style Throughout the Application

By Michael Detras

Example on how to apply a style to all the controls of the specified type in the application.

Creating a style throughout the application is easy. Just put the style in the resource dictionary of the Application. This can be done in App.xaml.

<Application
    
x:Class="WpfApplication1.App"
    
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
    
StartupUri="Window1.xaml">
    <Application.Resources>
<Style TargetType={x:Type CheckBox}>
<Setter Property="Focusable" Value="False"/>
</Style>
    </Application.Resources
>
</Application>

You can also write this in code. This could be done in the StartUp event handler of the application.

Style style = new Style(typeof(CheckBox));
style.Setters.Add(new Setter(UIElement.FocusableProperty, false));
App.Current.Resources.Add(typeof(CheckBox), style);

Apply a Style Throughout the Application  (896 Views)