zoukankan      html  css  js  c++  java
  • WPF TextBox文本为空时,显示水印

     public class TextBoxExtHelper
        {
     
            public static string GetIsNullString(DependencyObject obj)
            {
                return (string)obj.GetValue(IsNullStringProperty);
            }
    
            public static void SetIsNullString(DependencyObject obj, string value)
            {
                obj.SetValue(IsNullStringProperty, value);
            }
    
            // Using a DependencyProperty as the backing store for IsNullString.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty IsNullStringProperty =
                DependencyProperty.RegisterAttached("IsNullString", typeof(string), typeof(TextBoxExtHelper), new PropertyMetadata(""));
     
        }

    添加个附加属性,文本为空时,显示的水印名称

        <Style x:Key="TextBoxStyleWithWatermark" TargetType="{x:Type TextBox}">
               
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TextBox}">
                            <Border x:Name="Bg"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                                <Grid x:Name="PART_InnerGrid">
                                    
                                    <!--文本和水印-->
                                    <ScrollViewer x:Name="PART_ContentHost"
                                              BorderThickness="0"
                                              Grid.Column="1" 
                                              IsTabStop="False"
                                              Margin="2"
                                              VerticalAlignment="Stretch"
                                              Background="{x:Null}" />
                                    <TextBlock x:Name="WaterMark"
                                           Grid.Column="1"
                                           VerticalAlignment="Center"
                                           Foreground="Silver"
                                           FontSize="18"
                                           Text="{TemplateBinding txtExt:TextBoxExtHelper.IsNullString}"
                                           Visibility="Collapsed"
                                           Padding="5,0,0,0" />
     
                                </Grid>
                            </Border>
    
                            <ControlTemplate.Triggers>
                                <!--当Text为空时,隐藏删除按钮-->
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=Text}" Value="">
                                    <Setter Property="Visibility" TargetName="WaterMark" Value="Visible" />
                                </DataTrigger>
    
                                
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

    添加textbox的style

    用法如下:

    <TextBox x:Name="txt" Style="{StaticResource TextBoxStyleWithWatermark}" Width="120" Height="23" txtExt:TextBoxExtHelper.IsNullString="请输入文本"/>

  • 相关阅读:
    vue-搜索功能-实时监听搜索框的输入,N毫秒请求一次数据
    vue-注册全局过滤器
    vue-nuxt--切换布局文件
    vue.js 分页加载,向上滑动,依次加载数据。
    Vue.js项目引入less文件报错解决
    小程序/js监听输入框验证金额
    React 安装
    类垂直站点插件实现与分享
    多维度论怎样在日常中提升
    node.js的安装环境搭建
  • 原文地址:https://www.cnblogs.com/czly/p/12766940.html
Copyright © 2011-2022 走看看