有没有办法通过绑定更改ProgressBar
控件的颜色?
我知道我可以覆盖ProgressBarIndeterminateForegroundThemeBrush
资源,
但我需要在我的应用程序的不同页面上使用不同的颜色,并且这种方式是不可能的。
此外,无法使用Application.Current.Resources
来检索资源,我想通过设置画笔的Color
属性来创建行为。
答案 0 :(得分:1)
您可以编写扩展程序并将其(以某种方式)附加到您的页面。
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;
}
}
在Page1上使用它将画笔设置为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}">
并在第2页上将画笔设置为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}">
MyThemeColor1(X)和MyThemeColor2(Y)是您预定义的SolidColorBrush资源。例如:
<Application.Resources>
<SolidColorBrush x:Key="MyThemeColor1" Color="#cccc92" />
<SolidColorBrush x:Key="MyThemeColor2" Color="#3423ff" />
</Application.Resources>