Wpf根据build设置控件背景色

问题描述 投票:0回答:1

我有一个组合框,我通过编辑模板来设置背景颜色。该模板位于资源字典文件中。我有两种不同的构建配置,并且想根据其构建来设置背景颜色。我的想法是将样式中的背景绑定到标签或其他东西,并在后面的代码中设置颜色。这是如何实现的?有更好的方法吗?

MainWindow.xaml

<Grid>
    <ComboBox ItemsSource="{Binding ExpenseCategories}" Style="{StaticResource ComboBoxStyleGrayPurple}" Background="{Binding ComboBoxStaticBackground}" SelectedIndex="{Binding SelectedExcatIndex}" Width="150" HorizontalAlignment="Left" />
</Grid>

资源字典.xaml

这些是编辑模板中的相关行:

    <LinearGradientBrush x:Key="ComboBox.Static.Background" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#f8f1f9" Offset="0.0"/>
        <GradientStop Color="#e4c9e7" Offset="1.0"/>
    </LinearGradientBrush>

    <ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
        <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
            <Grid.ColumnDefinitions>
            ...
            </Grid.ColumnDefinitions>
            <Popup 
            ...
            </Popup>
            <ToggleButton x:Name="toggleButton" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource Mode=TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/>
            <ContentPresenter .../>
        </Grid>
        <ControlTemplate.Triggers>
         ...
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border x:Name="templateRoot" Background="{StaticResource ComboBox.Static.Background}" BorderBrush="{StaticResource ComboBox.Static.Border}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
                        <Border x:Name="splitBorder" BorderBrush="Transparent" BorderThickness="1" HorizontalAlignment="Right" Margin="0" SnapsToDevicePixels="true" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                            <Path x:Name="arrow" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z" Fill="{StaticResource ComboBox.Static.Glyph}" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
                        </Border>
                    </Border>
                    <ControlTemplate.Triggers>
                      ...
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我的想法是修改

ComboBoxToggleButton
->
Border
->
Background
中的行以绑定到
ComboBox
MainWndow.xaml
中的某些内容,然后在后面的代码中设置它,但我不是非常确定如何做到这一点。

MainWindowViewModel.cs

void SetValue()
{
#if BUILD2
    LinearGradientBrush build2Brush = new LinearGradientBrush(); // set build2 color
#else
    LinearGradientBrush build1Brush = new LinearGradientBrush(); // set build1 color
#endif
}

如何最好地实现这一点?

wpf triggers binding
1个回答
0
投票

您不得在视图模型中处理此类布局职责。
相反,将其移至代码隐藏(或一般视图)。

您还应该定义

Color
实例,而不是
Brush
(为了在 XAML 中配置和引用画笔时方便)。

以下示例为定义资源的

ResourceDictionary
创建一个代码隐藏文件。为了实现这一点,我们必须扩展
ResourceDictionary

MyResources.xaml

namespace Example
{
  using System.Windows;
  using System.Windows.Media;

  public partial class MyResources : ResourceDictionary
  {
#if DEBUG
    public static Color ConditionalGradientStopColor1 { get; } = (Color)ColorConverter.ConvertFromString("#f8f1f9");
    public static Color ConditionalGradientStopColor2 { get; } = (Color)ColorConverter.ConvertFromString("#e4c9e7");
#else
    public static Color ConditionalGradientStopColor1 { get; } = Colors.LightBlue;
    public static Color ConditionalGradientStopColor2 { get; } = Colors.DarkBlue;
#endif
  }
}

MyResources.xaml

<ResourceDictionary x:Class="Example.MyResources"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Example">

  <LinearGradientBrush x:Key="ComboBox.Static.Background"
                       EndPoint="0,1"
                       StartPoint="0,0">
    <GradientStop Color="{x:Static local:MyResources.ConditionalGradientStopColor1}"
                  Offset="0.0" />
    <GradientStop Color="{x:Static local:MyResources.ConditionalGradientStopColor2}"
                  Offset="1.0" />
  </LinearGradientBrush>

  <Style x:Key="TextBoxStyle" TargetType="TextBox">
    <Setter Property="Background"
            Value="{StaticResource ComboBox.Static.Background}" />
  </Style>
</ResourceDictionary>

然后照常使用一切:

MainWindow.xaml

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

  <TextBox Style={StaticResource TextBoxStyle}" />
</Window>
© www.soinside.com 2019 - 2024. All rights reserved.