WPF 使用TextBox做密码输入框

密码输入框需要输入的密码不能显示明文,用其他的特殊字符代替显示。
显示效果如下:
在这里插入图片描述

Xaml部分代码如下:

<Window x:Class="TextBoxPwd.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"
        xmlns:local="clr-namespace:TextBoxPwd"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox FontFamily="Courier New" x:Name="pwd" FontSize="20" Foreground="Transparent" Text="sdfsddfsfs"/>

        <TextBox FontFamily="Courier New"  FontSize="20"  Text="sdfsddfsfs"/>

    </StackPanel>
</Window>

后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TextBoxPwd
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            pwd.TextDecorations = new TextDecorationCollection(new TextDecoration[] {
                new TextDecoration() {
                     Location= TextDecorationLocation.Strikethrough,
                      Pen= new Pen(Brushes.Black, 10f) {
                          DashCap =  PenLineCap.Round,
                           StartLineCap= PenLineCap.Round,
                            EndLineCap= PenLineCap.Round,
                             DashStyle= new DashStyle(new double[] {0.0,1.2 }, 0.6f)
                      }
                }

            });

        }
    }
}

上面这种方式存在多种问题,直接使用WPF提供的控件也是可以的绑定部分也有方式实现。现在提供一种使用TextBox+Converter来实现的方式。
在这里插入图片描述
converter如下:

 public class PassWordConverter
        :IValueConverter
    {
        private string realWord = "";

        private char replaceChar = '*';

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(parameter != null)
            {
                string temp=parameter.ToString();
                if(!string.IsNullOrEmpty(temp))
                {
                    replaceChar= temp.First();
                }
            }

            if (value != null)
            {
                realWord = value.ToString();
            }
           

            string replaceWord = "";
            for(int index=0; index < realWord.Length; ++ index)
            {
                replaceWord += replaceChar;
            }

            return replaceWord;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string backValue = "";
            if(value != null)
            {
                string strValue = value.ToString();
                for(int index =0; index < strValue.Length;++index)
                {
                    if(strValue.ElementAt(index)== replaceChar)
                    {
                        backValue += realWord.ElementAt(index);
                    }
                    else
                    {
                        backValue += strValue.ElementAt(index);
                    }
                }
            }
            return backValue;
        }

    }

使用:

<Window x:Class="WpfApp2.Window4"
        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"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="Window4" Height="450" Width="800">
    <Window.Resources>
        <local:PassWordConverter x:Key="passWordConverter"/>
    </Window.Resources>
    <StackPanel Margin="30">
        <TextBox 
            FontSize="18"
            Height="30"
            Foreground="Red"            
            Text="{Binding Word,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource passWordConverter}}"/>
        <TextBox 
            FontSize="18"
            Height="30"
            Foreground="Red"            
            Text="{Binding Word,UpdateSourceTrigger=PropertyChanged}"/>
    </StackPanel>
</Window>


  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
WPF(Windows Presentation Foundation)是一种用于创建用户界面的框架。在WPF中实现动态搜索输入框可以通过以下步骤完成。 首先,我们需要在WPF界面中添加一个TextBox用于用户输入搜索关键字。然后可以为TextBox的TextChanged事件添加一个事件处理程序。在事件处理程序中,可以获取用户输入的关键字,并根据该关键字更新搜索结果。 接下来,我们需要在界面中添加一个列表或其他控件,用于显示搜索结果。当用户输入关键字时,我们可以在更新搜索结果时清空该列表,然后根据用户输入的关键字从数据库或其他数据源中查询相关的数据,并将查询结果显示在列表中。 为了实现动态搜索功能,我们可以通过使用LINQ等语言特性来执行查询操作。对于每个用户输入的字符,我们可以将其与数据库中的数据进行匹配,并使用LINQ查询语法来筛选出匹配的结果。然后将这些结果显示在搜索结果列表中。 在实现动态搜索时,我们还可以添加一些额外的功能,以提升用户体验。例如,可以采用自动匹配的方式,在用户输入关键字时自动显示匹配的结果。还可以添加一些搜索过滤条件,让用户可以根据自己的需要来进一步缩小搜索范围。 总之,使用WPF可以方便地实现动态搜索输入框。通过适当的事件处理和数据查询操作,我们可以根据用户输入的关键字动态更新搜索结果,并将结果显示在界面上,以满足用户的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值