I'm trying to implement a custom ProgressBar that displays the progress with text inside. I figured all by myself, and I even add an IFormatProvider to display content using your own format; however, I can't see the text. This is the code:
- Control:
public class InfoBar : ProgressBar
{
public string? Format { get; set; }
private TextBlock Info { get; } = new();
protected override Size ArrangeOverride(Size arrangeBounds)
{
if (Format is not null)
{
Info.Text = string.Format(new InfoBarFormatter(), $"{{0:{Format}}}", this);
Info.Arrange(new(0, 0, arrangeBounds.Width, arrangeBounds.Height));
}
return base.ArrangeOverride(arrangeBounds);
}
protected override Size MeasureOverride(Size constraint)
{
Info.Measure(constraint);
return base.MeasureOverride(constraint);
}
}
- WPF:
<Window x:Class="TestWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestWPF"
mc:Ignorable="d"
Title="MainWindow" Height="720" Width="1024">
<Grid>
<InfoBar Value="49.84759" Maximum="173.94852" Format="V2 / M2"/>
</Grid>
</Window>
The formatter works perfectly (V2 = Value rounded to 2 decimals, M2 = Maximum rounded to 2 decimals) and I have debugged a little bit and checked that the TextBlock inside arranges correctly, updating its width and height. The ProgressBar itself shows correclty.
I tried changing the arrange order (first arrange the base and then the TextBlock), but it didn't work. Additionaly, I messed up with the Z index values, but nothing worked.
P.S. I want to create a custom control, not do it b any other mean. I know there are XAML solutions like using a Grid with both, but I want to create the control so I can use it everywhere without using this "dirty" solution.