button.ActualHeight with IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj)

Blader 156 Reputation points
2020-09-28T16:40:57.297+00:00

I have the code bellow in Wpf C#. It works but problem is the first bt.ActualHeight.ToString() in message is always "0: All other values are right. Can you help me pleas

 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;
                    }
                }
            }
        }
  for (int i = 1; i < indexMax + 1; i++)
                {
                var btn = new Button()
                {
                    Content = i.ToString() + " " + textEnSRT[i] + " ",
                    HorizontalAlignment = HorizontalAlignment.Right,
                    Foreground = new SolidColorBrush(Colors.Black) { Opacity = 0.9 },
                    Background = new SolidColorBrush(Colors.LightGray) { Opacity = 0.4 },
                    BorderThickness = new Thickness(0, 0, 0, 0),
                    Margin = new Thickness(0, 2, 4, 0),
                    Tag = i,
                    FontSize = 16,                  
                 };
                btn.Click += Btn_Click;
                stackPanel1.Children.Add(btn);              
                }
            foreach (Button bt in FindVisualChildren<Button>(stackPanel1))
            {                
                MessageBox.Show(bt.Tag.ToString() + " " + bt.ActualHeight.ToString() + " " + bt.Content);
            }
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
{count} votes

Accepted answer
  1. Blader 156 Reputation points
    2020-09-29T16:23:25.477+00:00

    Thank you very much for your answer
    I have made a mistake. I wanted to simplify the code, so I put the code in the button click. In fact, the code should be as soon as the buttons are generated. That's when he makes the mistake. The first message is zero with the height of the button. I need to get the height of the btnHeigt [index] buttons. If I don't put a MessageBox in there, all btnHeigt [index] are zero.
    private void btnOpen_Click(object sender, RoutedEventArgs e)
    {
    for (int i = 1; i < indexMax + 1; i++)
    {
    var btn = new Button()
    {
    Content = i.ToString() + " " + textEnSRT[i] + " ",
    HorizontalAlignment = HorizontalAlignment.Right,
    Foreground = new SolidColorBrush(Colors.Black) { Opacity = 0.9 },
    Background = new SolidColorBrush(Colors.LightGray) { Opacity = 0.4 },
    BorderThickness = new Thickness(0, 0, 0, 0),
    Margin = new Thickness(0, 2, 4, 0),
    Tag = i,
    FontSize = 16,
    };
    btn.Click += Btn_Click;
    stackPanel1.Children.Add(btn);
    }
    // Now first bt.ActualHeight is zero;

         foreach (Button bt in FindVisualChildren<Button>(stackPanel1))
         {
             MessageBox.Show(bt.Tag.ToString() + " " + bt.ActualHeight.ToString() + " " + bt.Content);
         }
    

    // I need next. But now is all btnHeigt [indexK] are zero.
    ]
    foreach (Button bt in FindVisualChildren<Button>(stackPanel1))
    {
    btnHeight[indexK] = bt.ActualHeight.ToString();
    indexK++;
    }
    }

    Would you like help me
    Best Regards


0 additional answers

Sort by: Most helpful