我嘗試使用自定義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