I have a code bellow. I want to get heights from all buttons adding programmatically, but results for all heights is 0. Can you help me please?
int k = 1;
int indexMax = 100;
Double[] vyska = new double[1000];
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);
}
foreach (Button bt in FindVisualChildren<Button>(stackPanel1))
{
vyska[k] = bt.ActualHeight;
}
}
private void btnHladaj_Click(object sender, RoutedEventArgs e)
{
for (int i = 1; i < 10; i++)
{
MessageBox.Show(vyska[i].ToString()); //Every is zero.
}
}
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;
}
}
}
}