【问题标题】:XAML 2 ComboBoxes Color Styles with Resource Dictionary?XAML 2 ComboBoxes颜色样式与资源字典?
【发布时间】:2017-01-18 20:01:29
【问题描述】:

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

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

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

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

  • 组合框切换按钮
  • 组合框模板
  • ComboBoxEditableTemplate


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

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

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

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

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

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;

【问题讨论】:

    标签: c# wpf visual-studio xaml combobox


    【解决方案1】:

    您在 App.xaml 中声明资源

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

    或者如果你只在本地使用,就用这个

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

    【讨论】:

    • 移动到 对应用样式没有影响。 C# Brush FindResource 给出转换对象错误。 i.imgur.com/pZLEXfj.png 如果您愿意尝试,我已将示例上传到保管箱。无论如何制作一个红色,一个蓝色的方法。 dropbox.com/s/20mpnbi27xv7nny/ComboBoxColors.zip?dl=0
    • 您的问题出在“FindResource 函数返回 Style 类。您无法将其转换为 Brush。当您将其包装为 Brush 时,它必须为 null”
    • 我明白了,我像你展示的那样使用它,没有转换为画笔,它启动时没有错误,但它仍然没有将红色应用于组合框。
    • 您无法更改组合框背景样式,只需设置样式即可。因为组合框控件模板覆盖了您的样式。因此,您可以使用 Microsoft Blend 使用 Control Template 完全扩展 Combobox 样式。然后更改toggleButton 内的边框背景。如果需要,我可以添加完整模板
    • 如果您可以对示例保管箱文件执行此操作或将模板代码粘贴到某处,谢谢。
    【解决方案2】:

    在 XAML 中,像这样使用它,

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

    【讨论】:

    • 错误:“内容”属性只能设置一次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    相关资源
    最近更新 更多