private void CalculateFontSize() { double fontSize = this.MaxFontSize; List <TextBlock> tbs = VisualHelper.FindVisualChildren <TextBlock>(this.AssociatedObject); // get grid height (if limited) double gridHeight = double.MaxValue; Grid parentGrid = VisualHelper.FindUpVisualTree <Grid>(this.AssociatedObject.Parent); if (parentGrid != null) { RowDefinition row = parentGrid.RowDefinitions[Grid.GetRow(this.AssociatedObject)]; gridHeight = row.Height == GridLength.Auto ? double.MaxValue : this.AssociatedObject.ActualHeight; } foreach (var tb in tbs) { // get desired size with fontsize = MaxFontSize System.Windows.Size desiredSize = MeasureText(tb); double widthMargins = tb.Margin.Left + tb.Margin.Right; double heightMargins = tb.Margin.Top + tb.Margin.Bottom; double desiredHeight = desiredSize.Height + heightMargins; double desiredWidth = desiredSize.Width + widthMargins; // adjust fontsize if text would be clipped vertically if (gridHeight < desiredHeight) { double factor = (desiredHeight - heightMargins) / (this.AssociatedObject.ActualHeight - heightMargins); fontSize = Math.Min(fontSize, MaxFontSize / factor); } // get column width (if limited) if (AssociatedObject.ColumnDefinitions.Count > 0) { ColumnDefinition col = this.AssociatedObject.ColumnDefinitions[Grid.GetColumn(tb)]; double colWidth = col.Width == GridLength.Auto ? double.MaxValue : col.ActualWidth; // adjust fontsize if text would be clipped horizontally if (colWidth < desiredWidth) { double factor = (desiredWidth - widthMargins) / (col.ActualWidth - widthMargins); fontSize = Math.Min(fontSize, MaxFontSize / factor); } } } // apply fontsize (always equal fontsizes) foreach (var tb in tbs) { tb.FontSize = fontSize; } }