WPF 中PasswordBox控件的Password属性不能Binding问题解决方法
来源:互联网 发布:阿里云cdn和加带宽 编辑:程序博客网 时间:2024/05/18 01:47
最近用到了PasswordBox控件,但是发现Password属性不能Binding,因为它不是依赖属性,在网上找了找解决方法,自己做了小Demo,方便以后使用。
一、前台文件内容
<Window x:Class="PasswordBoxDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="142" Width="256"
xmlns:Helper="clr-namespace:PasswordBoxDemo"
WindowStartupLocation="CenterScreen">
<Grid>
<ListView x:Name="lvUsers"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
>
<ListView.View >
<GridView x:Name="GridView">
<GridView.Columns>
<GridViewColumn Header="用户名" >
<GridViewColumn.CellTemplate>
<DataTemplate >
<TextBox
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Text="{Binding Path=UserName}"
ToolTip="{Binding Path=UserName}"
Width="100"
Height="20"></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="密码" >
<GridViewColumn.CellTemplate>
<DataTemplate >
<PasswordBox
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Width="100"
Height="20"
PasswordChar="*"
MaxLength="20"
Helper:PasswordBoxHelper.Attach="True"
Helper:PasswordBoxHelper.Password="{Binding Path=Pwd,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
二、后台内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Collections.ObjectModel;
namespace PasswordBoxDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
ObservableCollection<User> users = new ObservableCollection<User>();
User p = new User();
p.UserName = "张三";
p.Pwd = "zhangsan";
User p2 = new User();
p2.UserName = "李四";
p2.Pwd = "lisi";
users.Add(p);
users.Add(p2);
this.lvUsers.ItemsSource = users;
}
}
public class User
{
private string _userName;
private string _password;
public string UserName
{
set { _userName = value; }
get { return _userName; }
}
public string Pwd
{
set { _password = value; }
get { return _password; }
}
}
}
三、帮助类 PasswordBoxHelper.cs 内容,代码摘自:http://www.wpftutorial.net/PasswordBox.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace PasswordBoxDemo
{
/// <summary>
/// 为PasswordBox控件的Password增加绑定功能
/// </summary>
public static class PasswordBoxHelper
{
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.RegisterAttached("Password",
typeof(string), typeof(PasswordBoxHelper),
new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
public static readonly DependencyProperty AttachProperty =
DependencyProperty.RegisterAttached("Attach",
typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false, Attach));
private static readonly DependencyProperty IsUpdatingProperty =
DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
typeof(PasswordBoxHelper));
public static void SetAttach(DependencyObject dp, bool value)
{
dp.SetValue(AttachProperty, value);
}
public static bool GetAttach(DependencyObject dp)
{
return (bool)dp.GetValue(AttachProperty);
}
public static string GetPassword(DependencyObject dp)
{
return (string)dp.GetValue(PasswordProperty);
}
public static void SetPassword(DependencyObject dp, string value)
{
dp.SetValue(PasswordProperty, value);
}
private static bool GetIsUpdating(DependencyObject dp)
{
return (bool)dp.GetValue(IsUpdatingProperty);
}
private static void SetIsUpdating(DependencyObject dp, bool value)
{
dp.SetValue(IsUpdatingProperty, value);
}
private static void OnPasswordPropertyChanged(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
passwordBox.PasswordChanged -= PasswordChanged;
if (!(bool)GetIsUpdating(passwordBox))
{
passwordBox.Password = (string)e.NewValue;
}
passwordBox.PasswordChanged += PasswordChanged;
}
private static void Attach(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
if (passwordBox == null)
return;
if ((bool)e.OldValue)
{
passwordBox.PasswordChanged -= PasswordChanged;
}
if ((bool)e.NewValue)
{
passwordBox.PasswordChanged += PasswordChanged;
}
}
private static void PasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
SetIsUpdating(passwordBox, true);
SetPassword(passwordBox, passwordBox.Password);
SetIsUpdating(passwordBox, false);
}
}
}
四、截图
五、参考博客地址:http://blog.csdn.net/fxhflower/article/details/6614831
- WPF 中PasswordBox控件的Password属性不能Binding问题解决方法
- WPF 为PasswordBox控件的Password增加绑定功能
- WPF PasswordBox.Password 的数据绑定
- wpf passwordbox 不能绑定问题
- WPF中radiobutton 的 data binding方法
- silverlight中PasswordBox控件的使用
- 如何绑定PasswordBox控制中的Password属性
- JSF中UI控件binding属性的用法
- WPF中Image控件的Source属性
- WPF中Border控件的属性介绍
- WPF 中Binding的2个属性Source与ElementName的区别
- wpf passwordbox输入框不能输入数字问题
- WPF中Binding的技巧(一)
- WPF中Binding的技巧(一)
- WPF中Binding的技巧(一)
- WPF在XAML中Binding使用StringFormat属性
- 潘鹏整理WPF(7)文本控件TextBox&&PassWordBox
- WPF密码控件PasswordBox之 MVVM绑定使用
- 黑马程序员——java IO输入与输出
- python 之 getopt的使用
- URL中#号(井号)的作用
- Rolling cURL: PHP并发最佳实践
- 摄氏温度转换华氏温度
- WPF 中PasswordBox控件的Password属性不能Binding问题解决方法
- POJ 1036 Gangsters
- C#解压、压缩RAR文件
- Linux下自动化部署和配置工具
- shell 脚本实现的守护进程
- Activity切换动画,Layout动画,View动画,供学习创新用
- 别了,google reader
- 智能计算与计算机网络
- Android 技术: 追踪vm 如何调用基础类