2017-02-18 37 views
-1

私はWPF MVVM Prism 6.2アプリケーションを記述します。ログインウィンドウ(PrismUserControl)のビューでは、ビューモデルで 'Password'プロパティにバインドされたPaswordBox(ビヘイビアを介して)があります。 アプリケーションの実行中にログインウィンドウが呼び出されるたびに、PasswordBoxが空でなければなりません。(たとえば、ユーザーが現在のセッションを閉じた後で、シェルの上に空のシェルとログインウィンドウしか表示されないようにする必要があります)私の問題は、上記のPasswordBoxがアプリケーションの読み込み後初めて初めて空になることです。 PaswordBoxが2番目または3番目のe.t.cに表示される場合、PaswordBoxは空ではありません。あなたはパスワードが空ではないですが、それはこの場合、空である必要があります見ることができるようにBehaviorを介してViewModelにバインドされたWPF PasswordBoxをクリアする方法は?

enter image description here

:下記の画像をご覧ください。以下

public class PasswordBoxBindingBehavior : Behavior<PasswordBox> 
{ 
    protected override void OnAttached() 
    { 
     AssociatedObject.PasswordChanged += OnPasswordBoxValueChanged; 
    } 

    public SecureString Password 
    { 
     get { return (SecureString)GetValue(PasswordProperty); } 
     set { SetValue(PasswordProperty, value); } 
    } 

    public static readonly DependencyProperty PasswordProperty = 
     DependencyProperty.Register("Password", typeof(SecureString), typeof(PasswordBoxBindingBehavior), new PropertyMetadata(null)); 

    private void OnPasswordBoxValueChanged(object sender, RoutedEventArgs e) 
    { 
     var binding = BindingOperations.GetBindingExpression(this, PasswordProperty); 
     if (binding != null) 
     { 
      PropertyInfo property = binding.DataItem.GetType().GetProperty(binding.ParentBinding.Path.Path); 
      if (property != null) 
       property.SetValue(binding.DataItem, AssociatedObject.SecurePassword, null); 
     } 
    } 
} 

され ':以下

. . . . . . . . . . . . . . 
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
. . . . . . . . . . . . . . 
<PasswordBox Grid.Row="1" Grid.Column="1" Height="30" Margin="0 10 5 0" AutomationProperties.AutomationId="UserPasswordBox"> 
     <i:Interaction.Behaviors> 
      <behavior:PasswordBoxBindingBehavior Password="{Binding Password}"/> 
     </i:Interaction.Behaviors> 
</PasswordBox> 
. . . . . . . . . . . . . . . . 

もすることができますSE上記のようにXAMLに関与している行動のクラスである:以下PaswordBoxは、ログインウィンドウのマークアップからXAMLスニペットはありますビューモデル内にある「パスワード」プロパティ。 PasswordBoxはPasswordBoxBindingBehaviorを経由して、このプロパティにバインドされています

public SecureString Password 
{ 
    get { return this._password; } 
    set { this.SetProperty(ref this._password, value); } 
} 

私はPasswordBoxは、ログインウィンドウは、アプリケーションの動作中に表示されるたびに空にすることが必要です。私はプログラムで 'ビュー'モデルで 'パスワード'プロパティをクリアしようとしましたが、それは役に立ちません。どうしたらいいですか?助けてください。

答えて

3

あなたは、ビューモデルのPasswordソースプロパティがnullに設定されている場合、空の文字列にPasswordBoxPasswordプロパティを設定し、あなたの行動のPassword依存関係プロパティのPropertyChangedCallbackをフックすることができます:

public class PasswordBoxBindingBehavior : Behavior<PasswordBox> 
{ 
    protected override void OnAttached() 
    { 
     AssociatedObject.PasswordChanged += OnPasswordBoxValueChanged; 
    } 

    public SecureString Password 
    { 
     get { return (SecureString)GetValue(PasswordProperty); } 
     set { SetValue(PasswordProperty, value); } 
    } 

    public static readonly DependencyProperty PasswordProperty = 
     DependencyProperty.Register("Password", typeof(SecureString), typeof(PasswordBoxBindingBehavior), new PropertyMetadata(OnSourcePropertyChanged)); 

    private static void OnSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     if(e.NewValue == null) 
     { 
      PasswordBoxBindingBehavior behavior = d as PasswordBoxBindingBehavior; 
      behavior.AssociatedObject.PasswordChanged -= OnPasswordBoxValueChanged; 
      behavior.AssociatedObject.Password = string.Empty; 
      behavior.AssociatedObject.PasswordChanged += OnPasswordBoxValueChanged; 
     } 
    } 

    private static void OnPasswordBoxValueChanged(object sender, RoutedEventArgs e) 
    { 
     PasswordBox passwordBox = sender as PasswordBox; 
     var behavior = Interaction.GetBehaviors(passwordBox).OfType<PasswordBoxBindingBehavior>().FirstOrDefault(); 
     if(behavior != null) 
     { 
      var binding = BindingOperations.GetBindingExpression(behavior, PasswordProperty); 
      if (binding != null) 
      { 
       PropertyInfo property = binding.DataItem.GetType().GetProperty(binding.ParentBinding.Path.Path); 
       if (property != null) 
        property.SetValue(binding.DataItem, passwordBox.SecurePassword, null); 
      } 
     } 
    } 
} 

次に、ビューモデルでPasswordソースプロパティをnullに設定するだけでPasswordBoxをクリアすることができます。

+0

mm8、ありがとうございました。パスワードは、ログインウィンドウが表示されるたびにクリアされます。 – Prohor

+1

これは期待どおりに動作しますか?答えを受け入れて投票することを忘れないでください:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – mm8

+0

これは非常に巧妙で、MVVMでPasswordBoxを使用することができます。アイデアとコードをありがとう – ktingle

関連する問題