shweta
shweta

Reputation: 49

How to apply style on a window in Xaml

I tried to apply style on a window in Xaml. But my code is not applying the style. Can anyone help me out in this problem?

<Window x:Class="Shweta.Window5"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window5" Height="300" Width="300">

    <Window.Resources>
        <Style  TargetType="{x:Type Window}">
            <Setter Property="FontFamily" Value="Verdana"></Setter>
            <Setter Property="Foreground" Value="LightBlue"></Setter>
            <Setter Property="FontWeight" Value="Normal"></Setter>
            <Setter Property="WindowStyle" Value="None"></Setter>
            <Setter Property="Background" Value="LightBlue"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="40*" />
            <ColumnDefinition Width="238*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="47*" />
            <RowDefinition Height="214*" />
        </Grid.RowDefinitions>

        <Grid Grid.Column="1">
            <Button Margin="0,0,114,16" Content="shweta"/>
        </Grid>
    </Grid>
</Window>

Upvotes: 3

Views: 3784

Answers (2)

Clemens
Clemens

Reputation: 128013

Instead of putting the Style into the Window's Resources, you could directly assign it to the Style property of the Window:

<Window x:Class="Shweta.Window5"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window5" Height="300" Width="300">
    <Window.Style>
        <Style TargetType="{x:Type Window}">
            <Setter Property="FontFamily" Value="Verdana"/>
            <Setter Property="Foreground" Value="LightBlue"/>
            <Setter Property="FontWeight" Value="Normal"/>
            <Setter Property="WindowStyle" Value="None"/>
            <Setter Property="Background" Value="LightBlue"/>
        </Style>
    </Window.Style>
    ...
</Window>

Upvotes: 3

Mighty Badaboom
Mighty Badaboom

Reputation: 6155

You have to put your style in the Resources of your App.xaml.

<Application.Resources>
    <Style  TargetType="{x:Type Window}">
        <Setter Property="FontFamily"
                Value="Verdana"></Setter>
        <Setter Property="Foreground"
                Value="LightBlue"></Setter>
        <Setter Property="FontWeight"
                Value="Normal"></Setter>
        <Setter Property="WindowStyle"
                Value="None"></Setter>
        <Setter Property="Background"
                Value="LightBlue"></Setter>
    </Style>
</Application.Resources>

When you define your Style in the Window it'll be for the children of the Window but not the Window itself.

Upvotes: 0

Related Questions