显示多行文本
TextBox控件通常存储单行文本(可通过设置MaxLenth属性来限制字符数量),然而,在许多情况下需要处理大量内容,从而会希望创建多行文本框,对于这种情况,可将TextWrapping属性设置为wrap或WrapWithOverflow.如果将TextWrapping属性设置为Wrap那么总是会在控件边缘换行,甚至将一个特别长的单词放在两行之中,如果将TextWrapping属性设置为WrapWithOverflow,这时如果换行算法没有发现合适的位置进行换行,就允许拉伸某些行使其超出右边缘
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox TextWrapping="Wrap">There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do. May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.</TextBox> </Grid> </Window>
控制多行文本行数
为了能自动在文本框中看到多行文本,需将其尺寸设置的足够大,不应当设置硬编码的高度,可使用方便的MinLines和MaxLines属性,MinLines属性是文本框中必须显示的最小行数,Maxlines属性设置文本框能够显示的最大行数,即使扩展文本框使其适合容器,也不会超过这个限制.
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox TextWrapping="Wrap" MaxLines="3">There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do. May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.</TextBox> </Grid> </Window>
显示滚动条
如果文本框支持换行,用户可输入更多能够立即在可视行中显示的文本,因此,通过将VerticalScrollBarVisibility属性设置为Visible或Auto,添加始终显示或按需显示的滚动条是有意义的
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox TextWrapping="Wrap" MaxLines="3" VerticalScrollBarVisibility="Auto">There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do. May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.</TextBox> </Grid> </Window>
支持enter和tab键
如果希望允许用户在多行文本框中通过按下enter键输入硬回车,为确保文本框支持Enter键,需要将AcceptReturn属性设置为true,也可以设置AcceptTabs属性,从而允许用户键入TAB键,否则Tab键焦点顺序将焦点移到下一个可得到焦点的控件上.
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" AcceptsReturn="True" AcceptsTab="True">There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do. May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.</TextBox> </Grid> </Window>
只读属性
有时,可能会创建纯粹为了显示文本的文本框,这时,可将IsReadOnly属性设置为true以阻止编辑文本,最好通过IsEnabled属性设置为False来禁用文本框,因为禁用文本框会显示变灰的文本,不支持文本选择,并且不支持滚动
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" AcceptsReturn="True" AcceptsTab="True" IsReadOnly="True" IsEnabled="False">There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do. May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.</TextBox> </Grid> </Window>