c# - XAML 2 组合框颜色样式与资源字典?

标签 c# wpf visual-studio xaml combobox

在@ebattulga的帮助下,这个问题已经解决。

我创建了一个包含多个模板覆盖的资源字典。 http://pastebin.com/nt3FxkM4

还有一个可供下载的 Visual Studio 项目示例 https://www.dropbox.com/s/20mpnbi27xv7nny/ComboBoxColors.zip?dl=0

以下键的名称末尾添加了红色和蓝色:

  • 组合框切换按钮
  • 组合框模板
  • 组合框可编辑模板

ComboBox Red & Blue


我有 2 个组合框。我需要制作一份红色和一份蓝色。

我在下面制作了一个示例项目。

在画笔属性中更改背景颜色不起作用。您需要覆盖默认模板样式 LinearGradientBrush ComboBox.Static.Background,它是灰色和白色的。

使用编辑模板,我可以覆盖默认画笔,将它们全部更改为红色。但我找不到一种方法来创建另一种风格来制作另一种蓝色。

我正在尝试资源字典,但样式没有生效。

ComboBoxes

XAML 2 组合框

<Window x:Class="ComboBoxColors.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <ResourceDictionary Source="ComboBoxStylesDictionary.xaml"/>
    </Window.Resources>

    <Grid>
        <ComboBox x:Name="ComboBoxRed" Style="{StaticResource ComboBoxRed}" HorizontalAlignment="Left" Margin="109,105,0,0" VerticalAlignment="Top" Width="120"/>
        <ComboBox x:Name="ComboBoxBlue" Style="{StaticResource ComboBoxBlue}" HorizontalAlignment="Left" Margin="286,105,0,0" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Window>

资源字典

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <LinearGradientBrush x:Key="ComboBoxRed.Static.Background" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="Red" Offset="0.0"/>
        <GradientStop Color="Red" Offset="1.0"/>
    </LinearGradientBrush>
    <SolidColorBrush x:Key="ComboBoxRed.Static.Border" Color="Red"/>
    <Style x:Key="ComboBoxRed" TargetType="{x:Type ComboBox}">
        <Setter Property="Background" Value="{StaticResource ComboBoxRed.Static.Background}"/>
        <Setter Property="BorderBrush" Value="{StaticResource ComboBoxRed.Static.Border}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
    </Style>

    <LinearGradientBrush x:Key="ComboBoxBlue.Static.Background" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="Blue" Offset="0.0"/>
        <GradientStop Color="Blue" Offset="1.0"/>
    </LinearGradientBrush>
    <SolidColorBrush x:Key="ComboBoxBlue.Static.Border" Color="Blue"/>
    <Style x:Key="ComboBoxBlue" TargetType="{x:Type ComboBox}">
        <Setter Property="Background" Value="{StaticResource ComboBoxBlue.Static.Background}"/>
        <Setter Property="BorderBrush" Value="{StaticResource ComboBoxBlue.Static.Border}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
    </Style>

</ResourceDictionary>

C#

我也尝试过这个,但找不到资源。

Brush ComboBoxRedStyle = (Brush)Application.Current.FindResource("ComboBoxRed");
ComboBoxRed.Background = ComboBoxRedStyle;

这没有效果。

ComboBoxRed.Background = Brushes.Red;

最佳答案

您在 App.xaml 中声明资源

<Application.Resources>
    <ResourceDictionary Source="ComboBoxStylesDictionary.xaml"/>
</Application.Resources>

或者,如果您只在本地使用它,请使用这个

Style ComboBoxRedStyle = (Style)this.FindResource("ComboBoxRed");

关于c# - XAML 2 组合框颜色样式与资源字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39432408/

相关文章:

c# - "Import"通过代码 ClassMapping 到 Fluent NHibernate

c# - 如何将命令绑定(bind)到祖先数据上下文? WPF::MVVM

c++ - 从 linux 交叉编译到 windows 的 visual studio 项目

c# - 在单独的解决方案中调试引用的 DLL

c - 修复 MSVC 编译器错误 C1074 : Illegal extension

c#:为什么不使用方法签名解决这个不明确的枚举引用?

c# - 不一致的 Silverlight 4 ComboBox 下拉显示

c# - 打印 double 值

wpf - 更改 XamDatGrid 的筛选器属性

.net - 找不到资源词典