如何更改 XAML 样式文件中的颜色



我有一个定义样式的进度条,我需要在运行时更改他的颜色(在样式中定义(。

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

这是我的样式(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>

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

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

Background="{DynamicResource LPercentBackground1Color}"

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

Resources["LPercentBackground1Color"] = Brushes.Green;

最新更新