0

I have several buttons, like this:

  <Button Name="btnContent" Grid.Column="0"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
                <Viewbox MaxWidth="100" StretchDirection="Both">
                    <TextBlock Text="Content" ></TextBlock>
                </Viewbox>
            </Button>

 <Button Name="btnMoreContent" Grid.Column="0"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
                <Viewbox MaxWidth="100" StretchDirection="Both">
                    <TextBlock Text="More Content" ></TextBlock>
                </Viewbox>
            </Button>

The font scales to fit the buttons. How can I grab the text size / scale value of the button with the smallest text at runtime and set them all to that size? I tried:

 double FontSize = btnContentButton.FontSize;
   if (FontSize < btnLongerContentButton.FontSize)
            {
                FontSize = btnLongerContentButton.FontSize;
            }

btnContentButton.FontSize = FontSize;
btnLongerContentButton.FontSize = FontSize;

But this doesn't work, because I never actually changed the font size - It sets them all to 12.

Patrick Schomburg
  • 2,494
  • 1
  • 18
  • 46

1 Answers1

0

Try the suggested solutions in this link and see if they can solve the problem.

However, you might want to try this one too: Handle Load event, find all TextBlocks with parent of type ViewBox (You might check other conditions or set proper names, to avoid further problems here), adjust their Width, properly, based on the Width of the one with the longest Text:

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        List<TextBlock> tbs = new List<TextBlock>();
        TextBlock longestText = null;
        foreach (TextBlock t in FindVisualChildren<TextBlock>(this))
        {
            if (t.Parent is Viewbox)
            {
                tbs.Add(t);
                if (longestText == null)
                    longestText = t;
                else if (t.Text.Length > longestText.Text.Length)
                    longestText = t;
            }
        }
        double a = longestText.ActualWidth / longestText.ActualHeight;
        foreach (TextBlock tb in tbs)
        {
            if (tb == longestText)
                continue;
            tb.Width = a * tb.ActualHeight;
        }

    }

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }

                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }

FindVisualChildren method is from here.

Hope it helps.

Community
  • 1
  • 1
rmojab63
  • 3,513
  • 1
  • 15
  • 28