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傳入,我們可以進入自己並在那裏定義寺廟,並且由於值已傳入,當值更新時,它會更新進度條的寺廟。