【c#】文本控件
一、TextBlock
1、分行
1.1 若内容超过长度,自动分行
TextWrapping="Wrap"
1.2 自定义分行
//文本内容使用Environment.NewLine进行拼接
this.tb.Text = "第一行内容"+ Environment.NewLine + "第二行内容";
2、截断省略
2.1
TextTrimming="CharacterEllipsis"
2.2 使用时需禁止Wrap,默认为NoWrap
TextWrapping="Wrap"
二、TextBox
1、一些属性
1.1 内容垂直对齐方式,默认为Top
VerticalContentAlignment="Top"//Center,Bottom,Stretch
1.2 最大长度
MaxLength = "";//中文、英文、其他符号都只占一个长度
1.3 文本自动换行
TextWrapping="Wrap"
1.4 Enter换行
AcceptsReturn="True"
2、样式
2.1 普通圆角
<Style TargetType="TextBox" x:Key="ThisTextBoxStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="border" CornerRadius="5" BorderBrush="#CCCCCC" BorderThickness="1" Padding="0,2" Height="{
Binding Width, RelativeSource={
RelativeSource Self}}" Background="{
TemplateBinding Background}">
<ScrollViewer VerticalAlignment="Top" x:Name="PART_ContentHost" DockPanel.Dock="Left" Background="{
TemplateBinding Background}" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled"/>
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{
Binding IsReadOnly,RelativeSource={
RelativeSource Self}}" Value="true">
<Setter Property="Background" Value="#f0f0f0">
</Setter>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
三、RichTextBox
1、
1.1
四、Placeholder
1、
1.1
五、一些示例
1、TextBoxWithErrorPop 带错误提示框的textBox
1.1 xaml
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Height" Value="32"></Setter>
<Setter Property="VerticalContentAlignment" Value="Center"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<Border x:Name="border" BorderBrush="#d6d6d6" BorderThickness="1" CornerRadius="5" Height="{
TemplateBinding Height}" Width="{
TemplateBinding Width}">
<Grid>
<TextBox x:Name="InTb" AcceptsReturn="True" Margin="4,0" Width="auto" Height="auto" BorderThickness="0" Text="{
Binding Text,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" FontSize="{
TemplateBinding FontSize}" Background="{
Binding Background,ElementName=border}" VerticalContentAlignment="{
TemplateBinding VerticalContentAlignment}" MaxLength="{
Binding MaxLength}" Visibility="Visible" TextWrapping="{
Binding TextWrapping}"></TextBox>
</Grid>
</Border>
<common:PopupNonTopmost IsOpen="{
Binding ErrOpen}" StaysOpen="True" x:Name="Pop_Err"
Placement="Bottom" PlacementTarget="{
Binding ElementName=border}"
AllowsTransparency="True" IsTopmost="False" HorizontalAlignment="Left">
<local:ErrorInfoPop Height="32" Width="250" Tag="{
TemplateBinding Tag}" ></local:ErrorInfoPop>
</common:PopupNonTopmost>
<!--<Popup AllowsTransparency="True" IsOpen="False" x:Name="Pop_Err">
<local:ErrorInfoPop Height="32" Width="250" Tag="{
TemplateBinding Tag}"></local:ErrorInfoPop>
</Popup>-->
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="VerticalContentAlignment" Value="top">
<Setter Property="Margin" TargetName="InTb" Value="4,6"></Setter>
</Trigger>
<DataTrigger Binding="{
Binding ErrOpen,RelativeSource={
RelativeSource Self}}" Value="false">
<Setter Property="BorderBrush" TargetName="border" Value="#d6d6d6"></Setter>
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{
Binding IsReadOnly,RelativeSource={
RelativeSource Self}