Is there a way to change the color of the ProgressBar
control with bindings? I know that I can override the ProgressBarIndeterminateForegroundThemeBrush
resource, but I need to have different colors on different pages of my app, and with this way it is not possible.
Also it is not possible to retreive the resource using Application.Current.Resources
which I wanted to create a behavior by setting the Color
property of the brush.
You could write an extension and attach it (in a way) to your page.
public class ProgressBarExtension
{
public static readonly DependencyProperty ProgressBarBrushProperty =
DependencyProperty.RegisterAttached("ProgressBarBrush",
typeof(Brush), typeof(ProgressBarExtension),
new PropertyMetadata(null, OnProgressBarBrushChanged));
public static void SetProgressBarBrush(UIElement element, object value)
{
element.SetValue(ProgressBarBrushProperty, value);
}
public static object GetProgressBarBrush(UIElement element)
{
return element.GetValue(ProgressBarBrushProperty);
}
private static void OnProgressBarBrushChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
App.Current.Resources["ProgressBarIndeterminateForegroundThemeBrush"] = args.NewValue as SolidColorBrush;
}
}
Use it on Page1 to set the brush to X:
<Page
x:Class="App1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
local:ProgressBarExtension.ProgressBarBrush="{StaticResource MyThemeColor1}">
and on Page2 to set the brush to Y:
<Page
x:Class="App1.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
local:ProgressBarExtension.ProgressBarBrush="{StaticResource MyThemeColor2}">
Where MyThemeColor1 (X) and MyThemeColor2 (Y) are SolidColorBrush resources you have predefined. For example:
<Application.Resources>
<SolidColorBrush x:Key="MyThemeColor1" Color="#cccc92" />
<SolidColorBrush x:Key="MyThemeColor2" Color="#3423ff" />
</Application.Resources>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.