Applying global styles to combobox

时间:2016-07-28 20:30:17

标签: wpf xaml combobox wpf-style

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 个答案:

答案 0 :(得分:0)

尽量避免App.Xaml中嵌套的ResourceDictionary。 以这种方式修复:

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

答案 1 :(得分:0)

我建议在您的解决方案中创建一个文件夹,并在其中添加一个Xaml控件:ResourceDictionary,您将在其中定义默认情况下要应用的所有全局样式。

例如:

<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>

现在,您只需要在App.Xaml中添加一个像这样的引用:

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

希望它会帮助你。

祝你有个美好的一天。