我是WPF的新手,并且尚未能找到解决方案。根据完成百分比更改样式的进度条
我们正试图使一个用户控件,提供了一个进度条,这将改变其风格为百分比上升(主要是红的时候像小于50%,黄色至30%等)
控制似乎为了更新风格而工作。当窗口首次启动时,即使进度条以50%左右的速度启动,该值始终为0。对我来说,似乎我弄乱了PropertyChanged代码或没有将数据连接到某处。这是迄今为止代码:
XAML文件comsuming的用户控件(TaskListStatus.xaml)
<ssw:ColoredProgressBar x:Name="pbCompleted" Value="{Binding PercentCompleted}" Height="40"/>
ColoredProgressBar.xaml:
<UserControl.Resources>
<this:ProgressBarStyleConverter x:Key="pbStyleConverter"/>
<!-- Progress Bar Styles-->
........
</UserControl.Resources>
<Grid>
<ProgressBar x:Name="pb" Value="{Binding Path=Value, ElementName=coloredBar}">
<ProgressBar.Style>
<Binding Converter="{StaticResource pbStyleConverter}"
RelativeSource="{RelativeSource Self}"/>
</ProgressBar.Style>
</ProgressBar>
</Grid>
ColoredProgressBar.xaml.cs
public partial class ColoredProgressBar : UserControl, INotifyPropertyChanged {
public ColoredProgressBar() {
InitializeComponent();
}
public static DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double),
typeof(ColoredProgressBar), new PropertyMetadata(null));
public double Value {
get { return Convert.ToDouble(GetValue(ValueProperty)); }
set {
SetValue(ValueProperty, value);
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("Value"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
ProgressBarStyleConverter.cs
public class ProgressBarStyleConverter : IValueConverter {
private const int RED_CUTOFF = 40;
private const int YELLOW_CUTOFF = 100;
private enum ProgressBarColor {
Green,
Yellow,
Red
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
FrameworkElement targetElement = value as FrameworkElement;
double progressBarValue = ((ProgressBar)targetElement).Value;
string styleName = "AeroProgressBarStyle";
ProgressBarColor color;
if (progressBarValue < RED_CUTOFF) {
color = ProgressBarColor.Red;
} else if (progressBarValue < YELLOW_CUTOFF) {
color = ProgressBarColor.Yellow;
} else {
color = ProgressBarColor.Green;
}
switch (color) {
case ProgressBarColor.Green:
styleName = "AeroProgressBarStyle"; break;
case ProgressBarColor.Yellow:
styleName = "YellowAeroProgressBarStyle"; break;
case ProgressBarColor.Red:
styleName = "RedAeroProgressBarStyle"; break;
}
return (Style)targetElement.TryFindResource(styleName);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return null;
}
}
如果有人能帮助它,将不胜感激。