2012-02-23 13 views

答えて

9

あなたは、次の例に示すように、doubleからBrushに変換value converterを使用することによって、そのValueプロパティにプログレスバーのForegroundプロパティをバインドすることができます。テストのために、ProgressBarのValueプロパティは、特にSliderコントロールのValueプロパティにバインドされていることに注意してください。

<Window.Resources> 
    <local:ProgressForegroundConverter x:Key="ProgressForegroundConverter"/> 
</Window.Resources> 
<StackPanel> 
    <ProgressBar Margin="10" 
       Value="{Binding ElementName=progress, Path=Value}" 
       Foreground="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Value, Converter={StaticResource ProgressForegroundConverter}}"/> 
    <Slider Name="progress" Margin="10" Minimum="0" Maximum="100"/> 
</StackPanel> 

結合値コンバータは、次のようになります。

public class ProgressForegroundConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     double progress = (double)value; 
     Brush foreground = Brushes.Green; 

     if (progress >= 90d) 
     { 
      foreground = Brushes.Red; 
     } 
     else if (progress >= 60d) 
     { 
      foreground = Brushes.Yellow; 
     } 

     return foreground; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

感謝をたくさんクレメンス。これは私が正確に探しているものです。 – Rani

+0

非常に古い答えは非常にばかげた質問かもしれませんが、これを実装しようとしましたが、値をプログラムで変更しようとするとプログレスバーが常に黒くなります。 –

関連する問題