TextBlock.xaml
<UserControl x:Class="Silverlight20.Control.TextBlock"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel HorizontalAlignment="Left">
<!--
Text - TextBlock上顯示的值
-->
<TextBlock Text="TextBlock" />
<!--
Foreground - 字型顏色
-->
<TextBlock Text="紅色的TextBlock" Foreground="Red" />
<!--
要以XAML的方式直接顯示引號等特殊字請使用相應的HTML編碼
-->
<TextBlock Text="帶引號的"TextBlock"" />
<!--
FontFamily - 字型
FontSize - 字型大小
FontWeight - 字形粗細度 [System.Windows.FontWeights枚舉]
FontStyle - 字形斜體否 [System.Windows.FontStyles枚舉]
TextDecorations - 字型底線 [System.Windows.TextDecorations枚舉]
FontStretch - 字型間距 [System.Windows.FontStretches枚舉]
-->
<TextBlock Text="常用屬性TextBlock" FontFamily="宋體" FontSize="36" FontWeight="Bold" FontStyle="Italic" TextDecorations="Underline" FontStretch="Normal" />
<!--
TextAlignment - 文字流向 [System.Windows.TextAlignment枚舉]
Run - 常值內容
LineBreak - 分行符號
LineHeight - 每行行高
TextWrapping - 文本限制(超過限制是否換行)
NoWrap - 永不換行
Wrap - 超過限制則換行
LineStackingStrategy - 控制行高的策略
MaxHeight - TextBlock內每行的行高 以LineHeight值 和 每行自身所設定的行高值 間的最大值為準
BlockLineHeight - TextBlock內每行的行高 以LineHeight值為準
-->
<TextBlock VerticalAlignment="Center" TextAlignment="Center" LineHeight="10" LineStackingStrategy="MaxHeight" Width="200" TextWrapping="NoWrap">
<Run FontSize="20" Foreground="Maroon" Text="MaroonMaroonMaroonMaroon" />
<LineBreak/>
<Run Foreground="Teal" Text="Teal" />
<LineBreak/>
<Run FontSize="30" Foreground="SteelBlue" Text="SteelBlue" />
</TextBlock>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" LineHeight="10" LineStackingStrategy="BlockLineHeight" Width="200" TextWrapping="Wrap">
<Run FontSize="20" Foreground="Red" Text="RedRedRedRedRedRedRedRedRed" />
<LineBreak/>
<Run Foreground="Green" Text="Green" />
<LineBreak/>
<Run FontSize="30" Foreground="Blue" Text="Blue" />
</TextBlock>
</StackPanel>
</UserControl>
TextBox.xaml
<UserControl x:Class="Silverlight20.Control.TextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel HorizontalAlignment="Left" Width="200">
<!--
Text - TextBox內顯示的值
BorderThickness - 邊框的寬度(像素值:上下左右;左右,上下;左,上,右,下)
BorderBrush - 邊框的顏色
-->
<TextBox Text="紅色框綠色底藍色字(真難看)" BorderBrush="Red" BorderThickness="1,5" Background="Green" Foreground="Blue" Margin="6" />
<!--
IsReadOnly - 是否唯讀
-->
<TextBox Text="唯讀TextBox" IsReadOnly="True" Margin="6" />
<!--
AcceptsReturn - 是否允許輸入斷行符號
HorizontalScrollBarVisibility - 水平捲軸的顯示狀態
VerticalScrollBarVisibility - 垂直捲軸的顯示狀態
Auto - 自動根據TextBox控制項的寬和高,以及其內容的寬和高,來決定是否顯示捲軸
Disabled - 不顯示,但是可以通過鍵盤或滑鼠在顯示內容中的移動或拖動操作,來看到被遮擋的內容
Hidden - 不顯示,而且無法看到被遮擋的內容
Visible - 顯示捲軸
-->
<TextBox Height="50" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" Margin="6"
Text="多行文字框1 多行文字框2 多行文字框3 多行文字框4 多行文字框5 多行文字框6" />
<!--
SelectionChanged - 選中的常值內容發生變化時所觸發的事件
-->
<TextBox Height="50" AcceptsReturn="False" Margin="5" SelectionChanged="TextBox_SelectionChanged"
Text="相應選中事件多行文字框1 多行文字框2 多行文字框3" />
<TextBlock Margin="5">
<Run>選中的文本為:</Run>
<LineBreak />
<Run x:Name="lblMsg"></Run>
</TextBlock>
</StackPanel>
</UserControl>
TextBox.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Silverlight20.Control
{
public partial class TextBox : UserControl
{
public TextBox()
{
InitializeComponent();
}
private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
lblMsg.Text = ((System.Windows.Controls.TextBox)sender).SelectedText;
}
}
}