点击一下按钮,我正在读取一个CSV文件,用“\ t”替换“,”并将其写入堆叠面板。
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)
{
foreach (TextBlock tb in sp.Children)
{
Size desiredSize = new Size();
tb.Measure(this.availableSize);
desiredSize = tb.DesiredSize;
}
}
}
从文件中,有些包含比其他字符串更长的字符串,因此标题中的TextBlocks比下面的TextBlocks更宽。
如何获取嵌入式StackPanel中最宽的TextBlock的宽度,并将嵌入的StackPanel中所有TextBlocks的宽度设置为?
答案 0 :(得分:2)
填充StackPanel
后,您就可以获得最大的Width
并为所有其他TextBox
设置它:
double largestWidth = stackPanel.Children.OfType<TextBox>().OrderByDescending(
textbox => textbox.ActualWidth).First().ActualWidth;
foreach (var textBox in stackPanel.Children.OfType<TextBox>())
{
textBox.ActualWidth = largestWidth;
}