0

有沒有辦法改變綁定的ProgressBar控件的顏色? 我知道我可以覆蓋ProgressBarIndeterminateForegroundThemeBrush資源, 但我需要在我的應用程序的不同頁面上使用不同的顏色,這樣就不可能。ProgressBar前景顏色綁定

此外,不可能通過使用Application.Current.Resources來獲取資源,我想通過設置畫筆的Color屬性來創建行爲。

回答

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; 
    } 
} 

使用它在第一頁的畫筆設置爲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}"> 

,並在第二頁設置畫筆爲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> 
+0

我想類似的東西,但它沒有工作,(我命名爲DP前景,所以用戶控制的前景財產被隱藏,並沒有叫我的回調方法),爲感謝反正回答,現在一切正如預期般運作 – 2014-09-29 13:18:18