简体   繁体   English

带有资源字典的 XAML 2 ComboBoxes 颜色样式?

[英]XAML 2 ComboBoxes Color Styles with Resource Dictionary?

With the help of @ebattulga this has been solved.在@ebattulga 的帮助下,这已经解决了。

I created a Resource Dictionary with multiple template overrides.我创建了一个具有多个模板覆盖的资源字典。 http://pastebin.com/nt3FxkM4 http://pastebin.com/nt3FxkM4

And an example visual studio project to download https://www.dropbox.com/s/20mpnbi27xv7nny/ComboBoxColors.zip?dl=0和一个示例视觉工作室项目下载https://www.dropbox.com/s/20mpnbi27xv7nny/ComboBoxColors.zip?dl=0

The following keys have Red and Blue added to the end of name:以下键在名称末尾添加了红色和蓝色:

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

组合框红色和蓝色


I have 2 ComboBoxes.我有 2 个组合框。 I need to make one Red and one Blue.我需要制作一个红色和一个蓝色。

I've made an example project below.我在下面做了一个示例项目。

Changing the background color in Brush Properties doesn't work.在画笔属性中更改背景颜色不起作用。 You need to override the Default Template Style LinearGradientBrush ComboBox.Static.Background which is grey and white.您需要覆盖灰色和白色的默认模板样式 LinearGradientBrush ComboBox.Static.Background。

Using Edit Template I can override the Default brush to change them all Red.使用编辑模板,我可以覆盖默认画笔以将它们全部更改为红色。 But I cannot find a way to create another Style to make the other Blue.但我找不到一种方法来创建另一种风格来制作另一种蓝色。

I'm trying Resource Dictionary, but the Style does not take effect.我正在尝试资源字典,但样式没有生效。

组合框

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

Resource Dictionary资源字典

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

I have also tried this, but resource not found.我也试过这个,但找不到资源。

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

And this with no effect.而这没有任何效果。

ComboBoxRed.Background = Brushes.Red;

You declare resource in App.xaml您在 App.xaml 中声明资源

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

Or if you only use it in locally, use this或者如果你只在本地使用它,使用这个

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

In XAML, use it like this,在 XAML 中,像这样使用它,

<Page>
    <Page.Resources>
        <ResourceDictionary Source="GiveFileName"/>
    </Page.Resources>
</Page>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM