In Silverlight, we often encounter line breaks when using labels, textblock, Textbox, and other controls. Here we use these three typical controls to look at the line breaks. In this article, we will divide the line feed into automatic line feed and manual line feed.
Automatic line feed
The label control does not support automatic line breaks in Silverlight.
Textblock and textbox can be automatically wrapped when the text exceeds the control width by setting the textwrapping = "Wrap" attribute.
Manual line feed
You can use the environment. newline attribute on the background to manually wrap labels, textblocks, and textbox controls.
Textblock can be stored in the XAMLCodeUse the <linebreak> label to manually wrap a line.
If you want to use the Enter key to wrap text in the textblock box, you need to set the acceptsreturn = "true" attribute.
First, let's look at the implementation of the automatic line feed XAML instance Code as follows:
<Border borderbrush = "blue" borderthickness = "1" width = "180" Height = "300"> <canvas> <textblock Height = "49" horizontalalignment = "Left" margin = ""42,111, 0, 0 "name =" textblock1 "text =" automatic line feed I will next line feed has been wrapped. "Verticalalignment =" TOP "textwrapping =" Wrap "width =" 120 "/> <textbox Height =" 60 "horizontalalignment =" Left "margin =" 42,192, 0, 0 "name =" textbox1 "verticalignment =" TOP "text =" automatically wrap the line. The next line has to be wrapped. "Textwrapping =" Wrap "acceptsreturn =" true "width =" 120 "/> </canvas> </Border>
Next, let's look at the code of the manual line feed XAML instance:
<Border borderbrush = "blue" margin = "300 0 50 50" borderthickness = "1" width = "180" Height = "300"> <canvas> <SDK: label Height = "45" horizontalalignment = "Left" margin = "42,10, 220 "name =" label2 "verticalignment =" TOP "width =" "/> <textblock Height =" 45 "horizontalalignment =" Left "margin =, 0, 0 "name =" textblock2 "text =" "verticalalignment =" TOP "width =" 178 "> <run> manually wrap your next line, </run> <linebreak/> <Ru N> line breaks. </Run> </textblock> <textblock Height = "45" horizontalalignment = "Left" margin = "42,110, 178 "verticalalignment =" TOP "name =" textblock3 "text =" "width =" "> </textblock> <textbox Height =" 45 "horizontalalignment =" Left "margin = ""42,160, 210 "name =" textbox2 "verticalignment =" TOP "acceptsreturn =" true "width =" "> </textbox> </canvas> </Border>
Finally, let's look at the implementation of the manual line feed XAML. CS Code as follows:
Public Partial Class Mainpage: usercontrol
{
Public Mainpage ()
{
Initializecomponent ();
This . Label2.content = " Manually wrap your next line, " + Environment. newline + " Line feed. " ;
This . Textbox2.text = " Manually wrap your next line, " + Environment. newline + " Line feed. " ;
This . Textblock3.text = " Manually wrap your next line, " + Environment. newline + " Line feed. " ;
}
}
If you need the source code of this article, click sltest.zip to download it. The implementation result of this instance is as follows: