3
私はすでに以下の質問をチェックしていますが、私はWPFに慣れていません。 Is there a way to change the color of a WPF progress bar via binding to a view model property現在の値が現在の値である範囲に応じて、その値に基づいてプログレスバーの前景色を変更する方法
サンプルがある場合は、私にご連絡ください。
私はすでに以下の質問をチェックしていますが、私はWPFに慣れていません。 Is there a way to change the color of a WPF progress bar via binding to a view model property現在の値が現在の値である範囲に応じて、その値に基づいてプログレスバーの前景色を変更する方法
サンプルがある場合は、私にご連絡ください。
あなたは、次の例に示すように、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();
}
}
感謝をたくさんクレメンス。これは私が正確に探しているものです。 – Rani
非常に古い答えは非常にばかげた質問かもしれませんが、これを実装しようとしましたが、値をプログラムで変更しようとするとプログレスバーが常に黒くなります。 –