public static int GetCharacterWidth(Grid container) { return container.Children.OfType<TextBlock>() .Where(textBlock => textBlock.Text != null) .Sum(textBlock => textBlock.Text.Length); }