这里我用的是vs2013进行学习的~
下面建立我们的第一个WPF项目,这里我取名WPFLearn
文本控件分为TextBlock,TextBox,PassWordBox,RichTextBox,下面我将 加上常用属性一 一演示。
TextBlock
<Window x:Class="WPFLearn.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">
<StackPanel>
<TextBlock Text="Hello WPF" FontFamily="宋体" FontSize="16" FontWeight="Bold"
Foreground="Red" TextWrapping="NoWrap" TextAlignment="Center"
TextTrimming="WordEllipsis">
</TextBlock>
<TextBlock Text="Hello WPF Hello WPF Hello WPF Hello WPF Hello WPF Hello WPF Hello WPF Hello WPF" FontFamily="宋体" FontSize="16" FontWeight="Bold"
Foreground="Red" TextWrapping="Wrap">
</TextBlock>
<TextBlock Text="Hello WPF Hello WPF Hello WPF Hello WPF Hello WPF Hello WPF Hello WPF Hello WPF" FontFamily="宋体" FontSize="16" FontWeight="Bold"
Foreground="Red" TextWrapping="NoWrap" TextTrimming="WordEllipsis">
</TextBlock>
</StackPanel>
</Window>
下面我来说下各个属性 Text:内容,FontFamily:字体,FontSize:字体大小,FontWeight:字体粗细 Foreground :前景色 。TextBlock专有属性 TextWapping是用来控制折行的,TextAlignment:对其方式, TextTrimming:省略方式 ,对比效果如下:
TextBox
<Window x:Class="WPFLearn.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 Margin="20" Height="100"
VerticalAlignment="Top" AcceptsReturn="True" AcceptsTab="True"
TextChanged="TextBox_TextChanged">
</TextBox>
</Grid>
</Window>
Margin:外边距 Height:高度 VerticalAlignment:垂直位置 AcceptsReturn:是否可以输入空格 AcceptTabs:是否可以输入Tab
TextChanged为一个当文本改变处理事件,你按下空格后会自动在.cs文件中添加代码,下面我们来编辑一下:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tb = (TextBox)sender;
MessageBox.Show(tb.Text);
}
效果如下:
PasswordBox
<Window x:Class="WPFLearn.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>
<PasswordBox PasswordChar="*" Password="12345"
PasswordChanged="PasswordBox_PasswordChanged">
</PasswordBox>
</Grid>
</Window>
PasswordChar:密码字符 Password:显示的密码
PasswordChanged为一个密码处理事件,在.cs文件中添加如下代码:
private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox pb = (PasswordBox)sender;
MessageBox.Show(pb.Password);
}
效果如下:
RichTextBox
需要注意的是RichTextBox不同于其他文本控件,它没有Text属性,它的内容呈现在FlowDocument中,里面又可以包括各种便签,这里我添加了一行文字跟一张图片!
<Window x:Class="WPFLearn.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>
<RichTextBox Height="300" Margin="10" >
<FlowDocument>
<Paragraph>
<Run>hello world</Run>
</Paragraph>
<BlockUIContainer>
<Image Source="ic_tubiao.png"></Image>
</BlockUIContainer>
</FlowDocument>
</RichTextBox>
</Grid>
</Window>
效果如下:
文本控件就到这里,大家一起学习,以后应该会有更详细的内容,希望大家支持!