silverlight中的textblock和textbox 使用之tips
关于 TextBlock 中 文本的换行:
1、使用前台XAML控制。
2、使用后台代码控制。
<TextBlock x:Name="text" Height="124" Margin="99,35,235,0" VerticalAlignment="Top" TextWrapping="Wrap">
<Run Foreground="#FF246300" Text="- Hello World! "/><LineBreak/>
<Run Text=" "/><Run Foreground="#FF000777" Text=" - Here is Lewis! "/><LineBreak/>
<Run Text=" "/><Run Foreground="#FF000777" Text=" - How "/>
<Run Foreground="#FF000777" Text="a"/>
<Run Foreground="#FF000777" Text="re "/>
<Run Foreground="#FF000777" Text="y"/>
<Run Foreground="#FF000777" Text="ou? "/><LineBreak/>
<Run Text=" "/>
<Run Foreground="#FF000777" Text=" - I'm "/>
<Run Foreground="#FF000777" Text="f"/>
<Run Foreground="#FF000777" Text="ine!"/></TextBlock>
tips: 1、可以看到,换行是通过 <LineBreak/>来控制的。
接着上面的示例,如何在后台设置,代码如下:
Run run = new Run();
run.Foreground = new SolidColorBrush(Colors.Orange);
run.Text = ("\nI'm a sentence added in c# code inside Inlines property!\nCan I be seen?");
// Choose one of three below to run:
// 1.No Text Property Setting Recover
// this.text.Inlines.Add(run);
//第一种情况:在原有的text后面追加内容
// 2.Text Property Setting Cover Setting in XAML
//this.text.Text = "\nI'm a sentence added in c# code inside Text property!\nCan I be seen?";
//this.text.Inlines.Add(run);
//第二种情况:替换掉原有的text ,后再追加内容
// 3.Text Property Setting Cover all Settings once
this.text.Inlines.Add(run);
this.text.Text = "\nI'm a sentence added in c# code inside Text property!\nCan I be seen?";
//第三种情况:Text属性覆盖了Run
这里展示了三种情况:
另外两个关于换行 的
http://www.dotblogs.com.tw/junegoat/archive/2010/08/10/17105.aspx
http://silverlightchina.net/html/tips/2010/1206/3913.html
http://silverlightchina.net/html/tips/2010/1124/3634.html
关于textbox: