【问题标题】:How to change a color in a XAML style file如何更改 XAML 样式文件中的颜色
【发布时间】:2020-01-09 08:17:12
【问题描述】:

我有一个已定义样式的 ProgressBar,我需要在运行时更改其颜色(在样式中定义)。

我一直在尝试寻找一个小时,但没有任何帮助解决我的问题

这是我的风格 (ProgressBar.xaml),我需要更改 LPercentBackground1Color 和 LPercentBackground2Color 的值或它们的使用位置

    <SolidColorBrush x:Key="LPercentBackground1Color" Color="Red" />
    <SolidColorBrush x:Key="LPercentBackground2Color" Color="White" />

    <Style x:Key="NormalStyle" TargetType="{x:Type ProgressBar}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ProgressBar}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Determinate" />
                                <VisualState x:Name="Indeterminate" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="PART_Track" Margin="0" BorderThickness="0" CornerRadius="7" />
                        <Border x:Name="PART_Indicator" Margin="0" BorderThickness="0" CornerRadius="5" HorizontalAlignment="Left"
                                    Background="{StaticResource LPercentBackground1Color}" ClipToBounds="True">
                            <Border x:Name="DiagonalDecorator" Width="5000">
                                <Border.Background>
                                    <DrawingBrush TileMode="Tile" Stretch="None" Viewbox="0,0,1,1" Viewport="0,0,36,34" ViewportUnits="Absolute">
                                        <DrawingBrush.RelativeTransform>
                                            <TranslateTransform X="0" Y="0" />
                                        </DrawingBrush.RelativeTransform>
                                        <DrawingBrush.Drawing>
                                            <GeometryDrawing Brush="{StaticResource LPercentBackground2Color}" Geometry="M0,0 -18,0 -36,34 -18,34 Z" />
                                        </DrawingBrush.Drawing>
                                    </DrawingBrush>
                                </Border.Background>
                            </Border>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我找不到如何改变这些,所以我需要你的帮助。谢谢!

【问题讨论】:

  • “我需要更改 LPercentBackground1Color 和 LPercentBackground2Color 的值或它们的使用位置” - 您能否说明更改颜色的目的是什么??
  • 目标是改变条形的颜色(这种样式使对角线) LPercentBackground1Color 是条形主色, LPercentBackground2Color 是对角线的颜色 所以我需要直接更改它们的值。或者我需要更改&lt;Border x:Name="PART_Indicator" [...] Background="[NEW COLOR]" ClipToBounds="True"&gt;这个
  • 如果你有一些模板预定义的颜色,并且你想在不同的视图中自定义它们,你可以看到this example

标签: c# wpf xaml dynamic


【解决方案1】:

如果您在模板中使用DynamicResource 标记扩展:

Background="{DynamicResource LPercentBackground1Color}"

...您可以在运行时替换ResourceDictionary 中的Brush:

Resources["LPercentBackground1Color"] = Brushes.Green;

【讨论】:

  • 我不知道为什么我之前想到了!谢谢你的工作,我已经坚持了很长一段时间:)
猜你喜欢
  • 2021-08-06
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-03
相关资源
最近更新 更多