我是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;
}
}
如果有人能幫助它,將不勝感激。