為了能實現PlaceHolder的效果,用 "https://www.cnblogs.com/catzhou/p/12658478.html" 的方式,實在是十分麻煩,要為每個文本框都折騰一把,實在不爽。現在介紹個簡單的方法,借用Tag屬性傳遞PlaceHolder的內容,代碼如下: 哦,順便弄了個 ...
為了能實現PlaceHolder的效果,用[https://www.cnblogs.com/catzhou/p/12658478.html]([WPF 學習] 12.動態綁定VisualBrush)的方式,實在是十分麻煩,要為每個文本框都折騰一把,實在不爽。現在介紹個簡單的方法,借用Tag屬性傳遞PlaceHolder的內容,代碼如下:
<Window.Resources>
<system:String x:Key="PlaceHolderEn">English</system:String>
<system:String x:Key="PlaceHolderCn">中文</system:String>
<ControlTemplate x:Key="TextBoxTemplatePlaceHolder" TargetType="TextBox">
<Border x:Name="border" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="True" CornerRadius="15" Padding="5,0">
<Grid>
<ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
<TextBlock x:Name="PlaceHolder" Text="{TemplateBinding Tag}" HorizontalAlignment="Center" VerticalAlignment="Center" Opacity="0" Background="Transparent"></TextBlock>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Text" Value="">
<Setter TargetName="PlaceHolder" Property="Opacity" Value="0.3"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<StackPanel Name="abc">
<TextBox FontSize="40" Text="" Tag="{DynamicResource PlaceHolderEn}" Name="aaa" Template="{StaticResource TextBoxTemplatePlaceHolder}"/>
<TextBox FontSize="40" Text="" Tag="{DynamicResource PlaceHolderCn}" Template="{StaticResource TextBoxTemplatePlaceHolder}"/>
</StackPanel>
</Window>
哦,順便弄了個圓角,那叫一個方便,爽!