WPF 使用TextBox做密码输入框

密码输入框需要输入的密码不能显示明文,用其他的特殊字符代替显示。
显示效果如下:
WPF 使用TextBox做密码输入框_第1张图片

Xaml部分代码如下:


    
        

        

    


后台代码如下:

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
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    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来实现的方式。
WPF 使用TextBox做密码输入框_第2张图片
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;
        }

    }

使用:


    
        
    
    
        
        
    



你可能感兴趣的:(控件)