我尝试使用自定义Behavior
将PasswordBox
的SecurePassword
属性绑定到我的ViewModel
。可悲的是,它不能正常工作。将SecurePassword绑定到ViewModel
基本上我加了一个属性到Behavior
其中包含我的ViewModel
的目标属性。
任何想法,为什么它不工作? PS:我目前在回家的路上没有我的笔记本电脑,我会在大约15分钟内用我的代码更新问题。但如果有人发表想法或者某事,会很好。
编辑
正如我说过的,这里是一些代码:)
的Behavior
第一:
using System;
using System.Collections.Generic;
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.Shapes;
using System.Windows.Interactivity;
using System.Security;
namespace Knerd.Behaviors {
public class PasswordChangedBehavior : Behavior<PasswordBox> {
protected override void OnAttached() {
AssociatedObject.PasswordChanged += AssociatedObject_PasswordChanged;
base.OnAttached();
}
private void AssociatedObject_PasswordChanged(object sender, RoutedEventArgs e) {
if (AssociatedObject.Password != null)
TargetPassword = AssociatedObject.SecurePassword;
}
protected override void OnDetaching() {
AssociatedObject.PasswordChanged -= AssociatedObject_PasswordChanged;
base.OnDetaching();
}
public SecureString TargetPassword {
get { return (SecureString)GetValue(TargetPasswordProperty); }
set { SetValue(TargetPasswordProperty, value); }
}
// Using a DependencyProperty as the backing store for TargetPassword. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TargetPasswordProperty = DependencyProperty.Register("TargetPassword", typeof(SecureString), typeof(PasswordChangedBehavior), new PropertyMetadata(default(SecureString)));
}
}
的PasswordBox
:
<PasswordBox Grid.Column="1" Grid.Row="1" Margin="5" Width="300" MinWidth="200">
<i:Interaction.Behaviors>
<behaviors:PasswordChangedBehavior TargetPassword="{Binding Password}" />
</i:Interaction.Behaviors>
</PasswordBox>
而在去年,该我的的一部分。
private SecureString password;
public SecureString Password {
get { return password; }
set {
if (password != value) {
password = value;
OnPropertyChanged("Password");
}
}
}
我希望任何人都可以帮忙,atm我使用codebehind版本,但我宁愿不。
EDIT 2
什么其实并不工作,该TargetPassword
属性不更新我的ViewModel
你的行为的一些代码将有助于了解您的问题;) – Dmitry
我希望他是如期 – MUG4N
就像我说的,在大约10分钟:) – Knerd