WPF学习开发历程(三)——文本控件

这里我用的是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>

效果如下:

文本控件就到这里,大家一起学习,以后应该会有更详细的内容,希望大家支持!






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值