ProgressBar with percent text
If you want a progress bar with text two colors you can use this
public class ProgressBarText : ProgressBar
{
static ProgressBarText()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ProgressBarText), new FrameworkPropertyMetadata(typeof(ProgressBarText)));
}
#region IndicatorColor (DP SHORT)
public Brush IndicatorColor { get { return (Brush)GetValue(IndicatorColorProperty); } set { SetValue(IndicatorColorProperty, value); } }
public static readonly DependencyProperty IndicatorColorProperty = DependencyProperty.Register("IndicatorColor",
typeof(Brush),
typeof(ProgressBarText),
new FrameworkPropertyMetadata(Brushes.Blue, FrameworkPropertyMetadataOptions.AffectsRender));
#endregion
#region IndicatorTextColor (DP SHORT)
public Brush IndicatorTextColor { get { return (Brush)GetValue(IndicatorTextColorProperty); } set { SetValue(IndicatorTextColorProperty, value); } }
public static readonly DependencyProperty IndicatorTextColorProperty = DependencyProperty.Register("IndicatorTextColor",
typeof(Brush),
typeof(ProgressBarText),
new FrameworkPropertyMetadata(Brushes.White, FrameworkPropertyMetadataOptions.AffectsRender));
#endregion
#region TextColor (DP SHORT)
public Brush TextColor { get { return (Brush)GetValue(TextColorProperty); } set { SetValue(TextColorProperty, value); } }
public static readonly DependencyProperty TextColorProperty = DependencyProperty.Register("TextColor",
typeof(Brush),
typeof(ProgressBarText),
new FrameworkPropertyMetadata(Brushes.Black, FrameworkPropertyMetadataOptions.AffectsRender));
#endregion
}
public class ProgressBarTextConverter : MarkupExtension, IMultiValueConverter
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
try
{
double total = (double)values[0];
double size = (double)values[1];
double calc= (size * 100 / total);
Console.WriteLine($"CALCUL = {calc}");
int v = (int)Math.Round(calc);
return $"{v} %";
}
catch { }
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
and style in generic.xaml
<Style TargetType="{x:Type local:ProgressBarText}">
<Setter Property="BorderBrush"
Value="gray" />
<Setter Property="BorderThickness"
Value="0.5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ProgressBarText}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Grid x:Name="PART_Track">
<TextBlock x:Name="PART_Text"
Grid.Column="1"
Foreground="{TemplateBinding TextColor}"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<TextBlock.Text>
<MultiBinding Converter="{local:ProgressBarTextConverter}">
<Binding Path="ActualWidth"
ElementName="PART_Track" />
<Binding Path="ActualWidth"
ElementName="PART_Indicator" />
</MultiBinding>
</TextBlock.Text></TextBlock>
</Grid>
<Border Background="{TemplateBinding IndicatorColor}"
VerticalAlignment="Stretch"
HorizontalAlignment="Left"
x:Name="PART_Indicator">
<Grid Width="{Binding ActualWidth, ElementName=PART_Track}">
<TextBlock Text="{Binding Text, ElementName=PART_Text}"
Foreground="{TemplateBinding IndicatorTextColor}"
Grid.Column="1"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
</Grid>
</Border>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
result for

code
<local:ProgressBarText Margin="10,112,9.6,0"
Height="41"
TextBlock.FontSize="24"
TextBlock.FontWeight="Bold"
TextColor="red"
IndicatorTextColor="Black"
VerticalAlignment="Top"
Value="49"
IndicatorColor="Lime" />
<local:ProgressBarText Margin="10,158,9.6,0"
Height="41"
TextBlock.FontSize="24"
TextBlock.FontWeight="Bold"
VerticalAlignment="Top"
Value="49" />
