绑定到SecurePassword视图模型 [英] Bind SecurePassword to ViewModel

查看:160
本文介绍了绑定到SecurePassword视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了 PasswordBox 我的视图模型 SecurePassword 属性绑定code>使用自定义的行为。可悲的是它不能正常工作。



基本上,我添加了一个属性添加到行为,其中包含的目标属性我的视图模型



任何想法,为什么它不工作?



PS:我目前在回家的路上没有我的笔记本电脑,我会用我的代码在大约15分钟更新的问题。但是,将是很好,如果有人张贴想法或某事。



修改



正如我说过的,这里是一些代码:)



行为第一:

 使用系统; 
使用System.Collections.Generic;
使用System.Text;使用System.Windows
;使用System.Windows.Controls的
;
使用System.Windows.Data;使用System.Windows.Documents
;
使用System.Windows.Input;使用System.Windows.Media
;
使用System.Windows.Media.Imaging;使用System.Windows.Shapes
;
使用System.Windows.Interactivity;
使用System.Security;

命名空间Knerd.Behaviors {
公共类PasswordChangedBehavior:行为< PasswordBox> {

保护覆盖无效OnAttached(){
AssociatedObject.PasswordChanged + = AssociatedObject_PasswordChanged;
base.OnAttached();
}

私人无效AssociatedObject_PasswordChanged(对象发件人,RoutedEventArgs E){
如果(AssociatedObject.Password!= NULL)
TargetPassword = AssociatedObject.SecurePassword;
}

保护覆盖无效OnDetaching(){
AssociatedObject.PasswordChanged - = AssociatedObject_PasswordChanged;
base.OnDetaching();
}

公共SecureString的TargetPassword {
{返回(SecureString的)的GetValue(TargetPasswordProperty); }
集合{的SetValue(TargetPasswordProperty,值); }
}

//使用的DependencyProperty作为后备存储TargetPassword。这使得动画,造型,绑定等等...
公共静态只读的DependencyProperty TargetPasswordProperty = DependencyProperty.Register(TargetPassword的typeof(SecureString的)的typeof(PasswordChangedBehavior),新PropertyMetadata(默认值(SecureString的)));
}
}



PasswordBox

 < PasswordBox Grid.Column =1Grid.Row =1保证金=5 WIDTH =300了minWidth =200> 
< I:Interaction.Behaviors>
<行为:PasswordChangedBehavior TargetPassword ={绑定密码}/>
< /我:Interaction.Behaviors>
< / PasswordBox>

和最后的部分我的视图模型

 私人SecureString的密码; 

公共SecureString的密码{
{返回密码; }
集合{
如果(密码=价值!){
密码=价值;
OnPropertyChanged(密码);
}
}
}



我希望有人能帮助, ATM我用的是代码隐藏版本,但我宁愿不会。



编辑2



其实什么不工作是,在 TargetPassword 属性没有更新我的视图模型 <财产/ p>

解决方案

我想我找到了一个有点奇怪的解决方案。请改善,如果有某物提高:)



我只是改变了它,像这样:



行为

 ; 
使用System.Collections.Generic;
使用System.Text;使用System.Windows
;使用System.Windows.Controls的
;
使用System.Windows.Data;使用System.Windows.Documents
;
使用System.Windows.Input;使用System.Windows.Media
;
使用System.Windows.Media.Imaging;使用System.Windows.Shapes
;
使用System.Windows.Interactivity;
使用System.Security;

命名空间Knerd.Behaviors {
公共类PasswordChangedBehavior:行为< PasswordBox> {

保护覆盖无效OnAttached(){
AssociatedObject.PasswordChanged + = AssociatedObject_PasswordChanged;
base.OnAttached();
}

私人无效AssociatedObject_PasswordChanged(对象发件人,RoutedEventArgs E){
如果(AssociatedObject.SecurePassword!= NULL)
AssociatedObject.DataContext = AssociatedObject.SecurePassword.Copy ();
}

保护覆盖无效OnDetaching(){
AssociatedObject.PasswordChanged - = AssociatedObject_PasswordChanged;
base.OnDetaching();
}

//使用的DependencyProperty作为后备存储TargetPassword。这使得动画,造型,绑定等等...
公共静态只读的DependencyProperty TargetPasswordProperty = DependencyProperty.Register(TargetPassword的typeof(SecureString的)的typeof(PasswordChangedBehavior),新PropertyMetadata(默认值(SecureString的)));
}
}



视图模型根本没有改变,但这里是我的查看

 < PasswordBox Grid.Column =1Grid.Row =1保证金=5WIDTH =300了minWidth =200的DataContext ={绑定密码,模式=双向}> 
< I:Interaction.Behaviors>
<行为:PasswordChangedBehavior />
< /我:Interaction.Behaviors>
< / PasswordBox>

这工作得尽善尽美,不会暴露明文密码。


I try to bind the SecurePassword property of a PasswordBox to my ViewModel with a custom Behavior. Sadly it doesn't work properly.

Basically I added a property to the Behavior which contains the target property of my ViewModel.

Any ideas why it doesn't work?

PS: I am currently on the way home without my laptop, I gonna update the question with my code in about 15 minutes. But would be nice if someone would post ideas or sth.

EDIT

As I promised, here is some code :)

The Behavior first:

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)));
    }
}

The 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>

And last, the part of my ViewModel.

private SecureString password;

public SecureString Password {
    get { return password; }
    set {
        if (password != value) {
            password = value;
            OnPropertyChanged("Password");
        }
    }
}

I hope anyone can help, atm I use the codebehind version but I rather wouldn't.

EDIT 2

What actually doesn't work is, that the TargetPassword property doesn't update the property of my ViewModel

解决方案

I think I found a kinda weird solution. Please improve if there is sth to improve :)

I just changed it like so:

The 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.SecurePassword != null)
                AssociatedObject.DataContext = AssociatedObject.SecurePassword.Copy();
        }

        protected override void OnDetaching() {
            AssociatedObject.PasswordChanged -= AssociatedObject_PasswordChanged;
            base.OnDetaching();
        }

        // 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)));
    }
}

The ViewModel didn't change at all, but here is my View:

<PasswordBox Grid.Column="1" Grid.Row="1" Margin="5" Width="300" MinWidth="200" DataContext="{Binding Password, Mode=TwoWay}">
    <i:Interaction.Behaviors>
        <behaviors:PasswordChangedBehavior />
    </i:Interaction.Behaviors>
</PasswordBox>

That works just perfect, without exposing the plaintext password.

这篇关于绑定到SecurePassword视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆