Search code examples
wpfwpf-style

Setting WPF style on Window


I wrote an XAML code in wpf, i defined a style in window.resourse like this

<Window x:Class="WpfApp1.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:self="clr-namespace:WpfApp1"
     xmlns:system="clr-namespace:System;assembly=mscorlib"
     xmlns:local ="clr-namespace:WpfApp1"
     mc:Ignorable="d"
     Height="400 " Width="450"  >

<Window.Resources>
    <Style TargetType="{x:Type Window}">
        <Setter Property="Title" Value="Hello my friens!"/>
    </Style>
</Window.Resources>

<Grid>
</Grid>

in here, Test is my window class name. when i run that everything is ok but when i changed above to this

 <Window.Resources>
    <Style TargetType="{x:Type Window}">
        <Setter Property="Title" Value="Hello my friends!"/>
    </Style>
</Window.Resources>

in design window, title showed as Value="Hello my friends!" but when i run the application, title become empty. what this happens? what is different btw TargetType="{x:Type Window}" and TargetType="{x:Type local:Test}" ?

did not every of them refer to window type ?


Solution

  • By just specifying the targettype in a style, the style automatically applies to all objects of the type you defined it for. However, that does not work for base classes.

    In your example, TargetType="{x:Type Window}" will automatically apply the title "Hello my friends!" to all windows. However, the type of your window is not Window, but WpfApp1.Test. Window is just the base class that is used. Thats why the style doesn't apply to the window automatically.

    If you use TargetType="{x:Type local:Test}" instead, it will apply automatically to all objects that have the type WpfApp1.Test, which is true for your window. The automatic applying of styles only works for the specific type, not the base class.

    You can also specify a key attribute, and then tell your window that it should use this style. In this case, you can also use x:Type Window, because then the style is being applied explicitly. e.g.:

    <Window x:Class="WpfApp1.Test"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:self="clr-namespace:WpfApp1"
         xmlns:system="clr-namespace:System;assembly=mscorlib"
         xmlns:local ="clr-namespace:WpfApp1"
         mc:Ignorable="d"
         Height="400 " Width="450" Style="{DynamicResource MyStyle}">
    
        <Window.Resources>
            <Style TargetType="{x:Type Window}" x:Key="MyStyle">
                <Setter Property="Title" Value="Hello my friends!"/>
            </Style>
        </Window.Resources>