使用wrap检测TextBlock中的换行符数量?

时间:2022-11-29 10:41:37

Is there any way to detect the number of lines breaks in a textblock with TextWrapping="Wrap"?

有没有办法用TextWrapping =“Wrap”来检测文本块中的换行符数?

I am considering using a non-monospaced font. I need this because I'm creating a new and personalized MessageBox Window, which has a big text title, animations, the logo of my application and the theme of my application.

我正在考虑使用非等宽字体。我需要这个,因为我正在创建一个新的,个性化的MessageBox窗口,它有一个大文本标题,动画,我的应用程序的徽标和我的应用程序的主题。

It's clear that I need to change the size of the window according to the number of LineBreaks of the body message - similar to how the default MessageBox window behaves.

很明显,我需要根据正文消息的LineBreak数量来改变窗口的大小 - 类似于默认MessageBox窗口的行为方式。

2 个解决方案

#1


5  

You can see how much txtName.ActualHeight you are getting with no wrap, and then divide the ActualHeight (with wrap) by the value you got earlier. You should get the number of lines.

你可以看到没有换行得到多少txtName.ActualHeight,然后用你之前得到的值除去ActualHeight(带换行)。你应该得到行数。

Note: you wont get the Actual height in the constructor. You will get it after the textblock get rendered in the form.

注意:您不会在构造函数中获得实际高度。在文本块在表单中呈现之后,您将获得它。

eg: (NoWrap)

例如:( NoWrap)

txt.ActualHeight
311.0

(Wrap)

(包)

txt.ActualHeight
1420.4400000000019

So,

所以,

int lineCount = (txt.ActualHeight / 311.0)

Hope it helps :)

希望能帮助到你 :)

Update as per your question update:

根据您的问题更新更新:

If you need to set messagebox height as per your textblock height, you can simply do this:

如果您需要根据文本块高度设置消息框高度,则可以执行以下操作:

msgbox.Height = txt.ActualHeight + 10;

// I added 10 just for adding a little margin.

//我添加了10只是为了增加一点余量。

#2


1  

Windows can adapt their size based on content. See the SizeToContent property.

Windows可以根据内容调整其大小。请参见SizeToContent属性。

#1


5  

You can see how much txtName.ActualHeight you are getting with no wrap, and then divide the ActualHeight (with wrap) by the value you got earlier. You should get the number of lines.

你可以看到没有换行得到多少txtName.ActualHeight,然后用你之前得到的值除去ActualHeight(带换行)。你应该得到行数。

Note: you wont get the Actual height in the constructor. You will get it after the textblock get rendered in the form.

注意:您不会在构造函数中获得实际高度。在文本块在表单中呈现之后,您将获得它。

eg: (NoWrap)

例如:( NoWrap)

txt.ActualHeight
311.0

(Wrap)

(包)

txt.ActualHeight
1420.4400000000019

So,

所以,

int lineCount = (txt.ActualHeight / 311.0)

Hope it helps :)

希望能帮助到你 :)

Update as per your question update:

根据您的问题更新更新:

If you need to set messagebox height as per your textblock height, you can simply do this:

如果您需要根据文本块高度设置消息框高度,则可以执行以下操作:

msgbox.Height = txt.ActualHeight + 10;

// I added 10 just for adding a little margin.

//我添加了10只是为了增加一点余量。

#2


1  

Windows can adapt their size based on content. See the SizeToContent property.

Windows可以根据内容调整其大小。请参见SizeToContent属性。