Windows Phone (Silverlight) 開發中,預設 PhoneTextBox 無內建前置圖示支援。本文示範繼承 PhoneTextBox 建立 ExtentPhoneTextBox,新增 TextHeadImage、TextHeadImageWidth/Height 相依屬性,並透過 ControlTemplate 完整重構視覺樹,實現高仿 QQ 登入頁的輸入框效果。
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace GMapWinFormDemo // 命名空間依專案調整
{
public class ExtentPhoneTextBox : PhoneTextBox
{
// 圖片來源
public static readonly DependencyProperty TextHeadImageProperty =
DependencyProperty.Register("TextHeadImage", typeof(ImageSource), typeof(ExtentPhoneTextBox), new PropertyMetadata(null));
public ImageSource TextHeadImage
{
get => (ImageSource)GetValue(TextHeadImageProperty);
set => SetValue(TextHeadImageProperty, value);
}
// 圖片寬度
public static readonly DependencyProperty TextHeadImageWidthProperty =
DependencyProperty.Register("TextHeadImageWidth", typeof(double), typeof(ExtentPhoneTextBox), new PropertyMetadata(24.0));
public double TextHeadImageWidth
{
get => (double)GetValue(TextHeadImageWidthProperty);
set => SetValue(TextHeadImageWidthProperty, value);
}
// 圖片高度
public static readonly DependencyProperty TextHeadImageHeightProperty =
DependencyProperty.Register("TextHeadImageHeight", typeof(double), typeof(ExtentPhoneTextBox), new PropertyMetadata(24.0));
public double TextHeadImageHeight
{
get => (double)GetValue(TextHeadImageHeightProperty);
set => SetValue(TextHeadImageHeightProperty, value);
}
}
}
<ResourceDictionary xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GMapWinFormDemo">
<!-- 標題樣板 -->
<DataTemplate x:Key="ControlHeaderTemplate">
<TextBlock FontSize="{StaticResource PhoneFontSizeNormal}"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
LineStackingStrategy="BlockLineHeight" LineHeight="23.333"
Margin="0,-9,0,0" TextWrapping="Wrap" Text="{Binding}">
<TextBlock.RenderTransform>
<TranslateTransform X="-1" Y="4"/>
</TextBlock.RenderTransform>
</TextBlock>
</DataTemplate>
<Style x:Key="ExtentPhoneTextBoxStyle" TargetType="local:ExtentPhoneTextBox">
<Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
<Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>
<Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>
<Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}"/>
<Setter Property="SelectionBackground" Value="{StaticResource PhoneAccentBrush}"/>
<Setter Property="SelectionForeground" Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}"/>
<Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
<Setter Property="Padding" Value="6,0,6,4"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ExtentPhoneTextBox">
<Grid x:Name="RootGrid" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="Header">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="ReadOnly">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxReadOnlyBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<!-- Header (標籤) -->
<ContentControl x:Name="Header" Grid.Row="0"
Content="{TemplateBinding Header}"
ContentTemplate="{StaticResource ControlHeaderTemplate}"
Foreground="{StaticResource PhoneSubtleBrush}"
Margin="0,0,0,8" />
<!-- 輸入區塊 -->
<Border x:Name="Border" Grid.Row="1"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/> <!-- 圖示欄 -->
<ColumnDefinition Width="*"/> <!-- 輸入框 -->
</Grid.ColumnDefinitions>
<!-- 前置圖示 -->
<Image x:Name="HeadImage" Grid.Column="0"
Source="{TemplateBinding TextHeadImage}"
Width="{TemplateBinding TextHeadImageWidth}"
Height="{TemplateBinding TextHeadImageHeight}"
Stretch="Uniform"
Margin="8,0,8,0"
Visibility="{TemplateBinding TextHeadImage, Converter={StaticResource NullToVisibilityConverter}}" />
<!-- 文字輸入 -->
<TextBox x:Name="TextBoxElement" Grid.Column="1"
Text="{TemplateBinding Text, Mode=TwoWay}"
Foreground="{TemplateBinding Foreground}"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
Padding="{TemplateBinding Padding}"
BorderThickness="0"
Background="Transparent"
SelectionForeground="{TemplateBinding SelectionForeground}"
SelectionBackground="{TemplateBinding SelectionBackground}"
VerticalAlignment="Center" />
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
public class NullToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value == null ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
在 App.xaml 或 ResourceDictionary 註冊:<local:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
<phone:PhoneApplicationPage ... xmlns:ctrl="clr-namespace:GMapWinFormDemo">
<phone:PhoneApplicationPage.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Styles/Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</phone:PhoneApplicationPage.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel Margin="24,40,24,0">
<TextBlock Text="QQ 登入" FontSize="48" Foreground="{StaticResource PhoneAccentBrush}" Margin="0,0,0,40"/>
<ctrl:ExtentPhoneTextBox x:Name="txtAccount"
Header="帳號"
TextHeadImage="/Assets/Images/ic_account.png"
TextHeadImageWidth="28" TextHeadImageHeight="28"
Style="{StaticResource ExtentPhoneTextBoxStyle}"
InputScope="Number" />
<ctrl:ExtentPhoneTextBox x:Name="txtPassword"
Header="密碼"
TextHeadImage="/Assets/Images/ic_lock.png"
TextHeadImageWidth="28" TextHeadImageHeight="28"
Style="{StaticResource ExtentPhoneTextBoxStyle}"
InputScope="Password" />
<Button Content="登入" Click="BtnLogin_Click"
Background="{StaticResource PhoneAccentBrush}"
Foreground="White" Margin="0,20,0,0" />
</StackPanel>
</Grid>
</phone:PhoneApplicationPage>
FocusStates 已內建邊框變色,可再加 ScaleTransform 輕微放大。Button (Visibility 綁定 Text.Length > 0),Click 清空 Text。Validation.ErrorTemplate 顯示紅色底線與提示。核心概念相同:建立繼承 TextBox 的自訂控制項、定義 IconSource 相依屬性、在 ControlTemplate 中以 Grid 排版圖示與文字輸入區。UWP 可直接使用 TextBox.HeaderTemplate 簡化。