zoukankan      html  css  js  c++  java
  • 【WPF】设置TextBox内容为空时的提示文字

    <TextBox Width="150" Margin="5">
        <TextBox.Resources>
            <VisualBrush x:Key="HintText" TileMode="None" Opacity="0.5" Stretch="None" AlignmentX="Left">
                <VisualBrush.Visual>
                    <TextBlock FontStyle="Italic" Text="请输入用户名"/>
                </VisualBrush.Visual>
            </VisualBrush>
        </TextBox.Resources>
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Style.Triggers>
                    <Trigger Property="Text" Value="{x:Null}">
                        <Setter Property="Background" Value="{StaticResource HintText}"/>
                    </Trigger>
                    <Trigger Property="Text" Value="">
                        <Setter Property="Background" Value="{StaticResource HintText}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

    运行效果:

    这里写图片描述

    注意点:

    • 不可直接设置TextBox的Text内容,否则将一直使用该Text内容。
  • 相关阅读:
    go学习中的零散笔记
    git reset --hard与git reset --soft的区别
    php必学必会
    gdb 解core
    php学习
    高仿京东到家APP引导页炫酷动画效果
    RxHttp
    SVN回滚文件
    遍历枚举
    python3 多线程
  • 原文地址:https://www.cnblogs.com/guxin/p/wpf-set-textbox-hint.html
Copyright © 2011-2022 走看看