首页 > 其他分享 >WPF实现placeholder效果

WPF实现placeholder效果

时间:2024-03-29 11:15:50浏览次数:15  
标签:效果 WatermarkTextBox 水印 typeof WPF 文本 placeholder public

 

概述:WPF中通过`Style`实现TextBox水印文本,使用`WatermarkTextBox`类及`ControlTemplate`。这个示例通过`VisualStateManager`在文本框失去焦点且内容为空时显示水印文本。通过`Watermark`属性简化水印文本设置,提高可维护性。

在WPF中,通过Style实现TextBox中的水印文本(水印、提示、占位符文本)通常使用ControlTemplate和VisualStateManager。以下是一个详细的实例源代码:

using System.Windows;
using System.Windows.Controls;

namespace WpfWatermarkExample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class WatermarkTextBox : TextBox
    {
        public static readonly DependencyProperty WatermarkProperty =
            DependencyProperty.Register("Watermark", typeof(string), typeof(WatermarkTextBox));

        public string Watermark
        {
            get { return (string)GetValue(WatermarkProperty); }
            set { SetValue(WatermarkProperty, value); }
        }

        static WatermarkTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WatermarkTextBox), new FrameworkPropertyMetadata(typeof(WatermarkTextBox)));
        }
    }
}

XAML文件:

<Window x:Class="WpfWatermarkExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:WatermarkTextBox Watermark="请输入内容" Width="200" Height="30"/>
    </Grid>
</Window>

Themes/Generic.xaml文件:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfWatermarkExample">

    <Style TargetType="{x:Type local:WatermarkTextBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:WatermarkTextBox}">
                    <Grid>
                        <TextBox x:Name="PART_TextBox"
                                 Text="{TemplateBinding Text}"
                                 VerticalAlignment="Center"
                                 HorizontalAlignment="Stretch"/>

                        <TextBlock x:Name="PART_Watermark"
                                   Text="{TemplateBinding Watermark}"
                                   Foreground="Gray"
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="Left"
                                   Visibility="Collapsed"/>

                    </Grid>

                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsFocused" Value="False"/>
                                <Condition Property="Text" Value=""/>
                            </MultiTrigger.Conditions>
                            <Setter TargetName="PART_Watermark" Property="Visibility" Value="Visible"/>
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

看效果:

 

这个例子中,自定义了WatermarkTextBox类,其中包含一个Watermark属性。在Themes/Generic.xaml中,定义了WatermarkTextBox的ControlTemplate,通过VisualStateManager在未获得焦点且文本为空时显示水印文本。在MainWindow.xaml中使用了WatermarkTextBox并设置了水印文本。

源代码获取:https://pan.baidu.com/s/1YCuKZhEOrs-C3nGqjNB-lA?pwd=6666 

 

标签:效果,WatermarkTextBox,水印,typeof,WPF,文本,placeholder,public
From: https://www.cnblogs.com/hanbing81868164/p/18103345

相关文章

  • 一个可以让你有更多时间摸鱼的WPF控件(一)
    前言我们平时在开发软件的过程中,有这样一类比较常见的功能,它没什么技术含量,开发起来也没有什么成就感,但是你又不得不花大量的时间来处理它,它就是对数据的增删改查。当我们每增加一个需求就需要对应若干个页面来处理数据的添加、修改、删除、查询,每个页面因为数据字段的差异需要单......
  • 抢先看!界面控件DevExpress WPF 2024产品路线图预览(一)
    DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。本文将介绍2024年DevExpressWPF第一个主要更新(v2......
  • 浅谈WPF之属性系统
    在WPF开发中,经常听到各种属性,如:依赖属性,附加属性,CLR属性,那这些不同类型的属性,具体又有什么作用呢?今天以一些简单的小例子,简述一下WPF开发中,各种属性的相关概念和应用,仅供学习分享使用,如有不足之处,还请指正。 CLR属性 CLR属性(CommonLanguageRuntime),又称为.Net标准属性,是......
  • HTML精美登录页面,(动态渐变效果+稍微透明效果)
    最近,学校留的html作业做出来十分简陋学校作业如上图所示,今天我来教大家做一个精美的登录页面。以下是精美的登录页面。HTML精美登录页面接下来我来带大家写代码一,HTML代码<bodyclass="meau"><divclass="formBox"><formaction=""class="FORMF">......
  • WPF绑定之道:为何选择属性而非字段,提升灵活性与可控性
     概述:WPF支持绑定到对象的属性而不是字段,主要因为属性提供了更多控制和扩展性。属性包含get和set方法,支持数据验证和通知属性更改,而字段通常被认为是内部实现。使用属性使WPF能够更灵活、可控地与数据交互,提高代码的可读性和可维护性。WPF(WindowsPresentationFoundation)支......
  • wpf draw rectangle with mouse
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;......
  • 可能是迄今为止最好用的WPF加载动画功能(没有之一)
    前言当我们在开发应用程序时,用户体验往往是至关重要的一环。在应用程序加载大量数据或执行复杂操作时,为用户提供一个良好的加载体验变得至关重要。加载动画是其中一个有效的方式,它不仅能够告知用户应用程序正在进行工作,还能够缓解用户在等待过程中的焦虑感。一.需求分析 ......
  • [C#] [WPF] MVVMToolkit入门案例心得
    跟着做的第一个MVVM项目,学到一点基础的东西,记下来;有些用词不准确假设我们要做一个页面,通过按钮来控制上方文本框的文字,通过勾选框来控制按钮的激活状态⬇️一般流程需要3个属性,2个私有属性,1个RelayCommand属性代表按钮点击后事件,并配有相应的getter/setter文本......
  • 记一次WPF的DataGrid绑定数据
    之前一直在用winform,但是感觉界面不好看,然后就自己在网上学习WPF。一开始看到DataGrid的时候,还以为它是DataGridView,然后用winform的方法绑定数据发现不行,在不断的查找之后,终于学会了怎么简单的绑定数据。工具:VStudio2022框架:.netframework4.8新建一个WPF窗体,再把DataGrid拖......
  • 【wpf】ListBoxItemIndexConverter转换器listbox序号自更新
    publicclassListBoxItemIndexConverter:IMultiValueConverter{publicobjectConvert(object[]values,TypetargetType,objectparameter,CultureInfoculture){stringindexPrefix=null;if(parameter!=null&&parameter......