[WPF 学习] 14.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>

[WPF 学习] 14.PlaceHolder的简单实现
哦,顺便弄了个圆角,那叫一个方便,爽!

[WPF 学习] 14.PlaceHolder的简单实现

上一篇:Winfrom 设置PC 端日期格式


下一篇:登录微软账号的Windows电脑如何远程?