2016-06-28 105 views
0

我寫了下面的代碼來顯示進度條和顯示百分比的文本。該文本塊將被放置在欄本身上。但現在字體是白色的。如果安裝進度沒有通過,我如何添加一個條件使其變爲黑色,如果有,那麼我需要將其更改爲白色。我怎樣才能做到這一點?請指教。如何根據WPF中的進度百分比更改進度欄文本顏色?

<ProgressBar Minimum="0" Maximum="100" Background="#FF1B1A1A" Value="{Binding Progress }" HorizontalAlignment="Stretch" Margin="20,182,20,121" Height="20" BorderBrush="#FF1B1A1A" > 
     <ProgressBar.Foreground> 
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
       <GradientStop Color="#FF0A92FB"/> 
       <GradientStop Color="#FF022F52" Offset="1"/> 
      </LinearGradientBrush> 
     </ProgressBar.Foreground> 

    </ProgressBar> 
    <TextBlock Name="progressLbl" Text="{Binding Progress,StringFormat={}{0}%}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="18,182,10,121" Width="40" Height="20" Foreground="#FFE6E6E6" FontSize="12" FontFamily="Segoe UI Light"/> 
+1

DataTrigger您的TextBlock迷上綁定到進度值的每種不超過/ GREATERTHAN轉換器應該利用它快速的工作。 –

回答

0

如下綁定TextBlock's Text財產,並使用DataTriggerConverterForeground

<TextBlock Text="{Binding Value, ElementName=PBar1}" Margin="92,216,159,54"> 
     <TextBlock.Style> 
      <Style TargetType="TextBlock"> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding Value, ElementName=PBar1, Converter={StaticResource ConvPercentageKey}}" Value="LessThan50"> 
         <Setter Property="Foreground" Value="Red"/> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding Value, ElementName=PBar1, Converter={StaticResource ConvPercentageKey}}" Value="MoreThan50"> 
         <Setter Property="Foreground" Value="Yellow"/> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </TextBlock.Style> 
    </TextBlock> 

轉換代碼:

public class PercentageConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     double v = Double.Parse(value.ToString()); 
     if (v < 50) 
      return "LessThan50"; 
     else 
      return "MoreThan50"; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
}