Get largest width of a textblock in a stackpanel
On a button click, I am reading through a CSV file, replacing ',' with
'\t' and writing it out to a stackpanel.
private void Button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < r.variables.Count; i++)
{
_people.Add(new TextBlock() { Text = r.variables[i],
HorizontalAlignment = System.Windows.HorizontalAlignment.Right
});//.ToString() });
StackPanel stp = new StackPanel() { Orientation =
Orientation.Vertical };
TextBlock tb = new TextBlock() {Text = r.variables[i]};
stp.Children.Add(tb);
_secondStack.Children.Add(stp);
}
foreach (StackPanel sp in _secondStack.Children)
{
List<double> maxWidth = new List<double>();
foreach (TextBlock tb in sp.Children)
{
Size desiredSize = new Size();
tb.Measure(this.availableSize);
desiredSize = tb.DesiredSize;
}
}
}
From the file, some contain strings that are longer than others, thus the
TextBlocks in the header are wider than those TextBlocks below. How do I
get the width of the widest TextBlock in the embedded StackPanel and set
the width of all TextBlocks inside the embedded StackPanel to that?
No comments:
Post a Comment