What is the best way to get the pixel width of all text entry fields?

Corbett Cappon 0 Reputation points
2023-04-04T23:52:33.4666667+00:00

Hello Microsoft, I am working on company software which uses WPF. We are currently working on translating all of the text of the program into another language. I would like to ensure that the translated text will not be cut off in the text fields of the program. I am thinking that the best way to do this is to determine pixel width of all text fields, then compare that to the pixel width of the translated text. Do you agree? If so, is there any convenient way to get the pixel width of all text fields?

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2023-04-05T07:21:19.41+00:00

    @Corbett Cappon, Welcome to Microsoft Q&A, you could try to use FormattedText class to get the pixel width of the text entry fields. Here is a code example you could refer to. xaml:

    <TextBlock x:Name="text1" Width="100" Height="50" VerticalAlignment="Top" Text="test1"></TextBlock>
    
    

    c# code:

    var formatted = new FormattedText(
                text1.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,
                new Typeface(text1.FontFamily, text1.FontStyle, text1.FontWeight, text1.FontStretch),
                             text1.FontSize, text1.Foreground, VisualTreeHelper.GetDpi(this).PixelsPerDip);
                Console.WriteLine(formatted.Width);
    

    we could see the actual pixel width: User's image

    Based on my research, it seems that there is no direct way to get all text entry controls. I am afraid that you may could try to use enum to do it, like the following:

    private void btntest_Click(object sender, RoutedEventArgs e)
            {
                //var formatted = new FormattedText(
                //text1.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,
                //new Typeface(text1.FontFamily, text1.FontStyle, text1.FontWeight, text1.FontStretch),
                //             text1.FontSize, text1.Foreground, VisualTreeHelper.GetDpi(this).PixelsPerDip);
                foreach (TextBlock tb in FindVisualChildren<TextBlock>(window1))
                {
                    // get textbolock pixel width
                    double d1 = GetWidth(tb);
                    Console.WriteLine(d1);
                }
                foreach (TextBox tb in FindVisualChildren<TextBox>(window1))
                {
                    // get textbox pixel width
                    double d1 = GetWidth(tb);
                    Console.WriteLine(d1);
                }
                foreach (PasswordBox tb in FindVisualChildren<PasswordBox>(window1))
                {
                    // get passwordbox pixel width
                    double d1 = GetWidth(tb);
                    Console.WriteLine(d1);
                }
    
            }
    
            public double GetWidth(TextBox box)
            {
                var formatted = new FormattedText(
                box.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,
                new Typeface(box.FontFamily, box.FontStyle, box.FontWeight, box.FontStretch),
                             box.FontSize, box.Foreground, VisualTreeHelper.GetDpi(this).PixelsPerDip);
                return formatted.Width;
            }
            public double GetWidth(TextBlock box)
            {
                var formatted = new FormattedText(
                box.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,
                new Typeface(box.FontFamily, box.FontStyle, box.FontWeight, box.FontStretch),
                             box.FontSize, box.Foreground, VisualTreeHelper.GetDpi(this).PixelsPerDip);
                return formatted.Width;
            }
            public double GetWidth(PasswordBox box)
            {
                var formatted = new FormattedText(
                box.Password, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,
                new Typeface(box.FontFamily, box.FontStyle, box.FontWeight, box.FontStretch),
                             box.FontSize, box.Foreground, VisualTreeHelper.GetDpi(this).PixelsPerDip);
                return formatted.Width;
            }
            public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
            {
                if (depObj == null) yield return (T)Enumerable.Empty<T>();
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject ithChild = VisualTreeHelper.GetChild(depObj, i);
                    if (ithChild == null) continue;
                    if (ithChild is T t) yield return t;
                    foreach (T childOfChild in FindVisualChildren<T>(ithChild)) yield return childOfChild;
                }
            }
    
    

    Hope my solution could be helpful for you. Best Regards, Jack


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.  


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.