c# - 如何计算在 WPF 应用程序的主窗口函数中分配了文本的文本框的总高度
问题描述
在我的应用程序中,堆栈面板有十个子文本框。我在 c# 脚本的主窗口函数中将文本分配给文本框。我想计算文本框的总高度。
问题是它给出了 0。请查看下面的代码:
List<TextInfoData> newList = new List<TextInfoData>();
public MainWindow()
{
InitializeComponent();
for (int num = 0; num < 10; num++)
{
newList.Add(new TextInfoData("Sunset is the time of day when our sky meets the " +
"outer space solar winds. There are blue, pink, and purple swirls, spinning " +
"and twisting, like clouds of balloons caught in a blender.", 1));
}
RearrangeTextData(newList);
}
private void RearrageTextData(List<TextInfoData> textInfoData)
{
TextBox tbox = new TextBox();
//rest of code to define textbox margin and setting textwrapping to wrap
double totalTextBoxHeight = 0;
foreach (TextInfoData tinfoData in textInfoData)
{
tbox.Text = tinfoData.GetTextDataString();
totalTextBoxHeight += tbox.ActualHeight;
rootStackPanel.Children.Add(tbox);
}
MessageBox.Show("Total Height: " + totalTextBoxHeight);
}
我有一个 TextInfoData 类,它接受字符串和整数两个值作为参数。有函数GetTextDataString,它返回字符串值。
父堆栈面板的名称是 rootStackPanel。
如果我检查 rootStackPanel 的子项总数,它显示十个(这是正确的)但是当我尝试获取总文本框高度时,它给出 0。请指导我。
解决方案
检查这篇文章:确定 WPF 文本块高度
计算可能是这样的:
private double GetHeight()
{
double height = 0;
foreach (var item in rootStackPanel.Children as IEnumerable)
{
if (item is TextBox tb)
{
tb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
height += tb.DesiredSize.Height;
}
}
return height;
}
希望有帮助。
其实我在这里又找到了一篇帖子。
推荐阅读
- angular - 将 ngx-pagination 实现为自定义组件
- android - Android 动画中的 fromXScale 和 fromYScale 是什么意思?
- android - 如何使用 volley 在 Expandable ListView 中映射 childView
- regex - 从字符串python中提取电话号码和传真号码
- python - 值的使用 >> i << i != 值:
- android - 强制 Firebase 动态链接在应用浏览器中打开
- c# - 字符串未被识别为有效的日期时间异常
- cas - 如何使用 LDAP 配置 CAS 5.3
- swisscomdev - Swisscom Application Cloud(公共)的邮件服务替代方案
- php - 获取解压缩的文件夹路径 - PHP