如何计算在WPF应用程序的主窗口函数中分配文本的文本框的总高度 [英] How to calculate the total height of textboxes whose text are assigned in the Main Window Function of a WPF Application

查看:54
本文介绍了如何计算在WPF应用程序的主窗口函数中分配文本的文本框的总高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好


在我的应用程序中,有十个文本框子框架。我在c#脚本的主窗口函数中为文本框分配文本。我想计算文本框的总高度。 


问题是它给出了0.请查看下面的代码:

 List< TextInfoData> newList = new List< TextInfoData>(); 
public MainWindow()  &NBSP; &NBSP; &NBSP; {&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;
InitializeComponent();  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;
for(int num = 0; num< 10; num ++)  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {
newList.Add(新的TextInfoData("日落"是我们的天空遇到外太空风的时候。有蓝色,粉红色和紫色的漩涡,旋转和扭曲,就像被吸入的气球云一个搅拌机。",1));  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;
}  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;
RearrangeTextData(newList);  &NBSP; &NBSP; &NBSP;
}
private void RearrageTextData(List< TextInfoData> textInfoData){

TextBox tbox = new TextBox();
//用于定义文本框边距和设置textwrapping以包装

double totalTextBoxHeight = 0的其余代码;

foreach(TextInfoData中的TextInfoData tinfoData){

tbox.Text = tinfoData.GetTextDataString();
totalTextBoxHeight + = tbox.ActualHeight;
rootStackPanel.Children.Add(tbox);

}
MessageBox.Show(" Total Height:" + totalTextBoxHeight);
}

我有一个TextInfoData类,它接受字符串和整数两个值作为参数。有函数GetTextDataString,它返回字符串值。 


如果我检查子项的总数,rootStackPanel,它显示十(这是正确的)但是当我试图获得总数时文本框高度,它给出0.请指导我。 




解决方案

您好Kumar,


如MSDN中所述"因为ActualWidth或ActualHeigth是计算值,应该知道,由于布局系统的各种操作,可能会有多个或增量报告的更改。布局系统可能正在为子元素计算
所需的度量空间,由父元素计算约束等等。"


在我们需要控件宽度的情况下有很多种情况在运行时为其他控件创建布局。我们真的不能依赖控件的Width属性,就像我们在之前的场景中看到的那样,Width与布局完成后控件获取的实际
宽度不同。我们无法确定在InitializeComponent()方法之后获取Window中任何控件的ActualWidth。



只应在LayoutUpdated事件之后检索ActualWidth属性窗口被执行。我们应该有一个附加到此事件的处理程序,如下所示。


因此,您可以在LayoutUpdated事件或TextBox.Load事件中获取TextBox.ActualHeight。


< pre class ="prettyprint"> public partial class Window23:Window
{
double totalTextboxheight = 0;
public Window23()
{
InitializeComponent();

for(int i = 0; i< 10; i ++)
{
TextBox text = new TextBox();
text.Text =" test" ;;
text.Loaded + = Text_Loaded;
stackpanel1.Children.Add(text);
}
Console.WriteLine(totalTextboxheight);
}

private void Text_Loaded(object sender,RoutedEventArgs e)
{
TextBox tb = sender as TextBox;
totalTextboxheight + = tb.ActualHeight;
Console.WriteLine(tb.ActualHeight);
Console.WriteLine(totalTextboxheight);
}
}

最好的问候,


Cherry




Hi

In my application, there are ten textboxes child to Stack panel. I assigning text to textboxes in the Main Window function of c# script. I want to calculate the total height of the textboxes. 

The problem is it is giving 0. Please look into the code below:



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);
}

I have a class TextInfoData which accepts string and integer two values as parameter. There is function GetTextDataString, which returns the string value. 

If I check total number of children, rootStackPanel, it is showing ten (which is right) but when I try to get total text box height, it gives 0. Please guide me. 

解决方案

Hi Kumar,

As stated in the MSDN "Because ActualWidth or ActualHeigth are calculated values, you should be aware that there could be multiple or incremental reported changes to it as a result of various operations by the layout system. The layout system may be calculating required measure space for child elements, constraints by the parent element and so on."

There are many cases when we need the width of the control at run time to create the layout for other controls. We really can't rely on the Width property of the control as we have seen in the previous scenarios where the Width is different from the actual width that the control acquires after the layout has been done. We cannot be sure about getting the ActualWidth of any control in the Window just after the InitializeComponent() method.

The ActualWidth property should only be retrieved after the LayoutUpdated event of the window is executed. We should have a handler attached to this event as shown below.

So you can get TextBox.ActualHeight in LayoutUpdated event or TextBox.Load event.

 public partial class Window23 : Window
    {
        double totalTextboxheight = 0;
        public Window23()
        {
            InitializeComponent();
           
            for (int i = 0; i < 10; i++)
            {
                TextBox text = new TextBox();              
                text.Text = "test";
                text.Loaded += Text_Loaded;              
                stackpanel1.Children.Add(text);
            }
            Console.WriteLine(totalTextboxheight);
        }
     
        private void Text_Loaded(object sender, RoutedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            totalTextboxheight += tb.ActualHeight;
            Console.WriteLine(tb.ActualHeight);
            Console.WriteLine(totalTextboxheight);
        }
    }

Best Regards,

Cherry


这篇关于如何计算在WPF应用程序的主窗口函数中分配文本的文本框的总高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆