C# ProgressBar前景色绑定

C# ProgressBar前景色绑定,c#,windows-runtime,windows-phone-8.1,windows-8.1,winrt-xaml,C#,Windows Runtime,Windows Phone 8.1,Windows 8.1,Winrt Xaml,有没有办法通过绑定更改ProgressBar控件的颜色? 我知道我可以覆盖progressBarIndeeterminateForRegroundTheMebrush资源, 但是我需要在我的应用程序的不同页面上有不同的颜色,用这种方式是不可能的 另外,无法使用Application.Current.Resources检索资源,我希望通过设置画笔的Color属性来创建该资源的行为。您可以编写扩展并将其(以某种方式)附加到页面 public class ProgressBarExtension {

有没有办法通过绑定更改
ProgressBar
控件的颜色? 我知道我可以覆盖
progressBarIndeeterminateForRegroundTheMebrush
资源, 但是我需要在我的应用程序的不同页面上有不同的颜色,用这种方式是不可能的


另外,无法使用
Application.Current.Resources
检索资源,我希望通过设置画笔的
Color
属性来创建该资源的行为。

您可以编写扩展并将其(以某种方式)附加到页面

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;
    }
}
在第1页使用它将笔刷设置为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>

我尝试了类似的方法,但没有成功,(我将DP Foreground命名为,因此用户控件的Foreground属性被隐藏,并且没有调用我的回调方法),无论如何,感谢您的回答,现在一切正常