ProgressBar前景颜色绑定
问题描述:
有没有办法改变绑定的ProgressBar
控件的颜色? 我知道我可以覆盖ProgressBarIndeterminateForegroundThemeBrush
资源, 但我需要在我的应用程序的不同页面上使用不同的颜色,这样就不可能。ProgressBar前景颜色绑定
此外,不可能通过使用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;
}
}
使用它在第一页的画笔设置为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>
我想类似的东西,但它没有工作,(我命名为DP前景,所以用户控制的前景财产被隐藏,并没有叫我的回调方法),为感谢反正回答,现在一切正如预期般运作 – 2014-09-29 13:18:18