0
votes

I'm trying to apply a global style to all ComboBoxes in my application. I'm doing this by defining a Style in my App.xaml file and specifying a TargetType, which should apply that style to all controls that are of the specified type. However, it appears that my style is not being applied at all.

Here's my code:

App.xaml

<Application x:Class="Test.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Test"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="{x:Type ComboBox}">
                <Setter Property="Background" Value="Red"></Setter>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindow.xaml

<Window x:Class="Test.MainWindow"
        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:local="clr-namespace:Test"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox Margin="173,130,186,166"></ComboBox>
    </Grid>
</Window>

I do not have any code-behind at this point other than the default code that VS generates for WPF forms.

I expect this XAML code to change the background of any ComboBoxes in any window to red, without me needing to manually specify the style for each ComboBox. (I really don't want write it out manually for each ComboBox - my app will end up using many, many CBs and it would be a major pain - not to mention it looks cluttered.)

I tried to model my code after this question but did not get any results.

2

2 Answers

1
votes

I would suggest to create a folder in your solution and add in it a Xaml control : ResourceDictionary where you gonna define all your global styles you want to apply by default.

For example :

<ResourceDictionary     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="Height" Value="25"/>
        <Setter Property="Background" Value="red"></Setter>
    </Style>
</ResourceDictionary>

Now, you just need to put a reference in you App.Xaml like this :

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Views/Style/GlobalStyle.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Hope it'll help you.

Have a good day.

0
votes

Try to avoid the nested ResourceDictionary in the App.Xaml. Fix in this way:

<Application.Resources>
    <Style TargetType="{x:Type ComboBox}">
        <Setter Property="Background" Value="Red"></Setter>
    </Style>
</Application.Resources>