0

I'm looking to apply styling to all WPF controls like what can be done with CSS in HTML.

This is my first dabbling in WPF and I've collected that I need to do this in App.xaml. Below is what I've got.

I've tried TargetType="{x:Type TabItem}" and TargetType="TabItem".

None of the styles I've defined are being applied.

App.xaml

<Application x:Class="VMware_Lab_Manager_Desktop.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary Source="Styles.xaml"></ResourceDictionary>
    </Application.Resources>
</Application>

Styles.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type TabControl}">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF464646" Offset="0" />
                    <GradientStop Color="#FF2D2D2D" Offset="0.5" />
                    <GradientStop Color="#FF141414" Offset="1" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type TabItem}">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF464646" Offset="0" />
                    <GradientStop Color="#FF2D2D2D" Offset="0.5" />
                    <GradientStop Color="#FF141414" Offset="1" />
                </LinearGradientBrush>

            </Setter.Value>
        </Setter>
        <Setter Property="BorderBrush">
            <Setter.Value>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="Black" Offset="0" />
                    <GradientStop Color="#FF464646" Offset="1" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type Window}">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="#FF2D2D2D" Offset="0.5" />
                    <GradientStop Color="#FF141414" Offset="0" />
                    <GradientStop Color="#FF464646" Offset="1" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

MainWindow.xaml

<Window x:Class="VMware_Lab_Manager_Desktop.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="456" Width="803" Padding="0" Margin="0">

    <TabControl HorizontalAlignment="Stretch" Margin="0" Padding="0" Name="tabControl1" VerticalAlignment="Stretch" BorderThickness="0" TabStripPlacement="Bottom">
        <TabItem Name="tabItem1" Header="...">
            <WebBrowser  HorizontalAlignment="Stretch" Name="webBrowser1" Margin="0" VerticalAlignment="Stretch" Source="http://www.msdn.com/" />
        </TabItem>
        <TabItem Header="+" />
    </TabControl>
</Window>
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • Where in your solution is Style.xaml located, the path in your App.xaml usally contains the namespace and location of the resourceDictionary to merge, e.g `` – sa_ddam213 May 19 '13 at 00:15
  • Styles.xaml is at the same level in solution explorer as App.xaml and MainWindow.xaml. I've aso tried putting the ResourceDictionary directly in App.xaml. – Andy Arismendi May 19 '13 at 00:21
  • Have you tried using MergedDictionary? I will post below because it wont fit in comment :) – sa_ddam213 May 19 '13 at 00:23

1 Answers1

2
 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Style.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • No dice. I think I'm missing something else. – Andy Arismendi May 19 '13 at 00:38
  • Strange, styles seem to show in the Visual Studio editor preview, but don't when the app is run. I put the project on github [here](https://github.com/aarismendi/VMware-Lab-Manager-Desktop). – Andy Arismendi May 19 '13 at 05:29
  • Sry, looks like some styles are there. For some reason the window and selected tab backgrounds aren't showing up. – Andy Arismendi May 19 '13 at 06:04
  • 1
    [This](http://stackoverflow.com/questions/4279773/wpf-window-style-not-being-applied) solved the window style not showing up. – Andy Arismendi May 19 '13 at 06:42
  • 1
    [This](http://www.dotnetspider.com/forum/320495-How-apply-selected-tab-Background-Color-WPF-TabControl.aspx) solved the selected tab background issue. Guess it really was working but these other issues tricked me :) – Andy Arismendi May 19 '13 at 07:15