【发布时间】: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 是对角线的颜色 所以我需要直接更改它们的值。或者我需要更改
<Border x:Name="PART_Indicator" [...] Background="[NEW COLOR]" ClipToBounds="True">这个 -
如果你有一些模板预定义的颜色,并且你想在不同的视图中自定义它们,你可以看到this example