2

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

如果有人能帮助它,将不胜感激。

回答

0

将样式绑定到ProgressBar的Value属性而不是ProgressBar本身。因此,将Path = Value属性添加到绑定中,并相应地修改转换器代码。

1

到目前为止,这是我们已经得到它的工作方式。我不是100%肯定这是理想的方式,但它的工作:

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> 
       <MultiBinding Converter="{StaticResource pbStyleConverter}"> 
        <Binding RelativeSource="{RelativeSource Self}" /> 
        <Binding ElementName="coloredBar" Path="Value" /> 
       </MultiBinding> 
      </ProgressBar.Style> 
     </ProgressBar> 
    </Grid> 

我们无法弄清楚如何获得绑定到一个单一的绑定。我们必须传递自我的原因是因为这是所有寺庙被定义为设置颜色的地方。我尝试将自身作为ConverterParameter传递,但我似乎无法使其工作。因此,通过将其作为MultiBinding传入,我们可以进入自己并在那里定义寺庙,并且由于值已传入,当值更新时,它会更新进度条的寺庙。