In theory, for this you can adjust the UserControl and a little code-behind. To measure the width of the text without displaying suitable class FormattedText . Here is a simple sketch:
Control:
<UserControl x:Class="Test.DoubleWidthFitControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <TextBlock Name="Target" HorizontalAlignment="Right"/> </UserControl>
Code-behind:
public partial class DoubleWidthFitControl : UserControl { public DoubleWidthFitControl() { InitializeComponent(); SizeChanged += (o, args) => Recalc(); } #region dp double Value public double Value { get { return (double)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( "Value", typeof(double), typeof(DoubleWidthFitControl), new PropertyMetadata(0.0, (o, args) => ((DoubleWidthFitControl)o).Recalc())); #endregion void Recalc() { var targetWidth = ActualWidth; var num = Value; string s; // у C# 7 есть локальные функции, вау! bool CheckLength(string format) { s = num.ToString(format); return Measure(s) <= targetWidth; } if (CheckLength("F") || CheckLength("G")) { Target.Text = s; return; } int i; for (i = 0; CheckLength($"E{i}"); i++) /**/; if (i == 0) Target.Text = "###"; else Target.Text = num.ToString($"E{i - 1}"); } double Measure(string s) { TextBlock tb = Target; var formattedText = new FormattedText( s, CultureInfo.CurrentCulture, tb.FlowDirection, new Typeface(tb.FontFamily, tb.FontStyle, tb.FontWeight, tb.FontStretch), tb.FontSize, Brushes.Black); return formattedText.Width; } }
It’s easy to use:
<local:DoubleWidthFitControl Value="10000000000" />
Result:

There is a good potential for further optimization. For example, with a given line and a font, you can once calculate and remember the display options with their sizes, and simply select the largest one that fits.