首页 > Windows开发 > 详细

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

时间:2020-04-21 14:25:26      阅读:77      评论:0      收藏:0      [点我收藏+]

为了能实现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的简单实现

原文:https://www.cnblogs.com/catzhou/p/12744307.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!