TextBlock, TextBox, PasswordBox, RichEd
介紹
重新想象 Windows 8 Store Apps 之文本控制項
TextBlock - 文本顯示框
TextBox - 文本輸入框
PasswordBox - 密碼輸入框
RichEditBox - 富文本編輯框
RichTextBlock - 富文本顯示框
RichTextBlockOverflow - 溢出文本顯示框
樣本
1、TextBlock 的 Demo
TextBlockDemo.xaml
<Page x:Class="XamlDemo.Controls.TextBlockDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <!-- TextBlock 的常用屬性 顯示的文本如果是引號等特殊字需要使用相應的 HTML 編碼 --> <TextBlock Text="TextBlock "的常用屬性"" TextAlignment="Left" Foreground="Blue" FontFamily="微軟雅黑" FontSize="24" FontWeight="Bold" FontStyle="Italic" FontStretch="Normal" TextWrapping="Wrap" /> <!-- CharacterSpacing - 用於設定字元間距 具體字元間隔像素計算公式如下:字型大小 * CharacterSpacing值 / 1000 = 字元間距像素值 LineHeight - 行高 LineStackingStrategy - 控制行高的策略 MaxHeight - 每行的行高以 LineHeight 值和每行的自然行高中的最大值為準。預設值 BlockLineHeight - 每行的行高以 LineHeight 值為準,以字元地區為參考 BaselineToBaseline - 每行的行高以 LineHeight 值為準,以基準為參考(什麼是基準:英文字元的基準基本相當於單詞本4條線中的第3條線) Inlines - 內嵌元素的集合。包括 span, bold, italic, underline 等,但是 InlineUIContainer 不可用,其需要在 RichTextBlock 中使用 --> <TextBlock FontSize="24" CharacterSpacing="100" LineStackingStrategy="MaxHeight" LineHeight="100"> <TextBlock.Inlines> <Run Foreground="Red">Run</Run> <Span Foreground="Green">Span</Span> <LineBreak /> <Bold>Bold</Bold> <Italic>Italic</Italic> <Underline>Underline</Underline> </TextBlock.Inlines> </TextBlock> <!-- TextTrimming - 文字溢出時的顯示方式 TextTrimming.None - 不做任何處理 TextTrimming.WordEllipsis - 在邊界處,用省略符號代替剩餘文本 --> <TextBlock FontSize="24" HorizontalAlignment="Left" Text="abcdefghijklmnopqrstuvwxyz" Width="200" TextTrimming="WordEllipsis" /> <!-- FrameworkElement.FlowDirection - 指定文本或介面元素在它們的父元素中的流動方向 FlowDirection.LeftToRight - 內容從左至右流動(預設值) FlowDirection.RightToLeft - 內容從右至左流動 --> <TextBlock FontSize="24" HorizontalAlignment="Left" Text="abcdefg" Width="200" FlowDirection="RightToLeft" /> <!-- IsTextSelectionEnabled - 文本是否可以被選中 --> <TextBlock FontSize="24" Name="lblSource" IsTextSelectionEnabled="True" SelectionChanged="txt_SelectionChanged_1"> <TextBlock.Inlines> <Run>abcdefg</Run> <LineBreak /> <Run>hijklmn</Run> <LineBreak /> <Run>opqrst</Run> </TextBlock.Inlines> </TextBlock> <!--顯示 lblSource 中被使用者選中的文本(這裡的綁定不起作用,應該是bug,所以具體實現放到cs裡了)--> <TextBlock FontSize="24" Name="lblCopy" Text="{Binding SelectedText, ElementName=lblSource}" /> </StackPanel> </Grid> </Page>