WPF PasswordBox MVVM 实现
2018-06-17 21:01:31来源:未知 阅读 ()
由于PasswordBox.Password属性非依赖属性,所以不能作为绑定的目标,以下是本人的MVVM实现方法。
PasswordBox.Password与TextBox.Text同步,TextBox只是为了演示,实际使用的是TextBox.Text绑定的Source。
通过Behavior添加PasswordChanged事件的事件处理器,以及自定义一个附加属性实现绑定,Behavior需要引用System.Windows.Interactivity.dll,代码如下:
using System.Windows; using System.Windows.Controls; using System.Windows.Interactivity; namespace PasswordHelperTest { public static class PasswordBoxHelper { public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordBoxHelper), new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged)); private static void OnPasswordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { PasswordBox passwordBox = sender as PasswordBox; string password = (string)e.NewValue; if (passwordBox != null && passwordBox.Password != password) { passwordBox.Password = password; } } public static string GetPassword(DependencyObject dp) { return (string)dp.GetValue(PasswordProperty); } public static void SetPassword(DependencyObject dp, string value) { dp.SetValue(PasswordProperty, value); } } public class PasswordBoxBehavior : Behavior<PasswordBox> { protected override void OnAttached() { base.OnAttached(); AssociatedObject.PasswordChanged += OnPasswordChanged; } private static void OnPasswordChanged(object sender, RoutedEventArgs e) { PasswordBox passwordBox = sender as PasswordBox; string password = PasswordBoxHelper.GetPassword(passwordBox); if (passwordBox != null && passwordBox.Password != password) { PasswordBoxHelper.SetPassword(passwordBox, passwordBox.Password); } } protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.PasswordChanged -= OnPasswordChanged; } } }
View代码如下:
<Window x:Class="PasswordHelperTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:local="clr-namespace:PasswordHelperTest" Title="MainWindow" Height="350" Width="525"> <StackPanel> <PasswordBox Margin="3" local:PasswordBoxHelper.Password="{Binding Path=Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> <i:Interaction.Behaviors> <local:PasswordBoxBehavior /> </i:Interaction.Behaviors> </PasswordBox> <TextBox Margin="3" Text="{Binding Path=Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox> </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 PasswordHelperTest { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new MainWindowViewModel(); } } }
ViewModel引用Microsoft.Practices.Prism.dll,代码如下:
using Microsoft.Practices.Prism.ViewModel; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace PasswordHelperTest { class MainWindowViewModel : NotificationObject { private string _password; public string Password { get { return _password; } set { if (_password != value) { _password = value; this.RaisePropertyChanged("Password"); } } } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:页面导航
下一篇:ADO.NET数据库
- wpf,vb,位图剪裁的方法 2018-06-23
- WPF(C#)与MATLAB混合编程 2018-06-17
- WPF MVVM 之理解(数据绑定) 2018-06-17
- Wpf中鼠标样式的修改,作用点修改 2018-06-17
- WPF多线程UI更新——两种方法 2018-06-17
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash