Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF window style not being applied

Tags:

styles

wpf

I have a ResourceDictionary that contains Style definitions for controls used within my application.

All of the styles are applied properly to the controls in the window...but the style in the ResourceDictionary for the window itself is not being applied.

This is the XAML in my ResourceDictionary that contains the style that I want to apply to my window:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:primatives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type Window}">
        <Setter Property="Background" Value="#FF121212"></Setter>
        <Setter Property="Height" Value="768"></Setter>
        <Setter Property="Width" Value="1024"></Setter>
    </Style>
<!-- .... -->
</ResourceDictionary>

This is the XAML for the window that I am working with (trying to get this style to apply):

<Window x:Class="TryingStyles"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TryingStyles">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/StylesDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>    
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="Label1" VerticalAlignment="Top" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="56,14,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TabControl Height="206" HorizontalAlignment="Left" Margin="12,43,0,0" Name="TabControl1" VerticalAlignment="Top" Width="250">
                <TabItem Header="TabItem1" Name="TabItem1">
                    <Grid></Grid>
                </TabItem>
            </TabControl>
            <GroupBox Header="GroupBox1" Margin="268,43,12,12" Width="396"></GroupBox>
        </StackPanel>
    </StackPanel>
</Window>

It appears that the style for the window is applied when I view the window in the IDE's "Design view" but when I run the application the style is not applied.

Does anyone know what I'm doing wrong?

like image 385
Frinavale Avatar asked Nov 25 '10 18:11

Frinavale


1 Answers

It's very strange that it works with the designer but not when the application runs. The problem seems to be the TargetType of your Style. Wpf seems not be able to match the Window class with your derivated class TryingStyles.

Change your TargetType and it will work :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:primatives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:my="clr-namespace:WpfApplication1">
    <Style TargetType="{x:Type my:TryingStyles}">
        <Setter Property="Background" Value="#FF121212"></Setter>
        <Setter Property="Height" Value="768"></Setter>
        <Setter Property="Width" Value="1024"></Setter>
    </Style>
    <!-- .... -->
</ResourceDictionary>
like image 80
Nicolas Avatar answered Dec 07 '22 12:12

Nicolas