从XAML中定义的ListBoxItems的ListBox中的PasswordBox中获取密码 [英] Get Password from PasswordBox in ListBox of ListBoxItems defined in XAML

查看:61
本文介绍了从XAML中定义的ListBoxItems的ListBox中的PasswordBox中获取密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF ListBox,每个ListBoxItem都有许多UIElements.这些UIElements之一是PasswordBox.这些UIElementsXaml ControlTemplate中定义.

I have a WPF ListBox, each ListBoxItem has many UIElements. One of those UIElements is PasswordBox. These UIElements are defined in Xaml ControlTemplate.

我需要做的是在后面的代码中从PasswordBox获取一个Password字符串.但是我不能绑定PasswordBoxPassword属性,这不是动态属性,因此很有意义,因为Password不应长时间存储在内存中.

What I need to do is get a Password string from PasswordBox in code behind. But I can not bind Password Property of PasswordBox, which is not a dynamic property and it make sense as Password should not be stored in a memory for long.

现在我想到的解决方案是,在后面的代码中获取ListBoxItemUIElements并从那里获取Password.

Now the Solution that comes to my mind is to get UIElements of ListBoxItem in code behind and get Password from there.

但是我无法弄清楚如何从ListBox中获取代码中的UIElements.

But I am unable to figure out how can i get the UIElements in code behind from ListBox.

推荐答案

密码框的密码"属性不是依赖项属性,因此无法绑定此属性.

The Password property of the password box is not is dependency property, hence you can not bind this property.

您可以通过以下两种方式之一从密码框控件中获取密码.

You can us either of the following way to get the password from passwordbox control.

  1. 将密码框控件作为参数传递给item随附的命令.

  1. Pass the password box control as a parameter to the command attached with item.

XAML

<Window x:Class="WpfApplication4.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
<StackPanel>
    <PasswordBox Name="txtPassword" VerticalAlignment="Top" Width="120" />
    <Button Content="Ok" Command="{Binding Path=OkCommand}" CommandParameter="{Binding ElementName=txtPassword}"/>
</StackPanel>

背后的代码

public class MyViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        public ICommand OkCommand { get; private set; }

        public MyViewModel()
        {
            OkCommand = new GeneralCommand<object>(ExecuteOkCommand, param => CanExecuteCommand());
        }

        private void ExecuteOkCommand(object parameter)
        {
            var passwordBox = parameter as PasswordBox;
            var password = passwordBox.Password;
        }

        private bool CanExecuteCommand()
        {
            return true;
        }
    }

这有点违反MVVM模式,因为现在视图模型已经知道了有关视图实现方式的一些信息.

This slightly violates the MVVM pattern since now the viewmodel knows something about how the view is implemented.

  1. 使用attach属性绑定密码.

XAML

<StackPanel>
    <PasswordBox w:PasswordHelper.Attach="True" 
         w:PasswordHelper.Password="{Binding Text, ElementName=plain, Mode=TwoWay}" 
                 Width="130"/>
    <TextBlock Padding="10,0" x:Name="plain" />
</StackPanel>

代码

public static class PasswordHelper
{
    public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.RegisterAttached("Password",
        typeof(string), typeof(PasswordHelper),
        new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));

    public static readonly DependencyProperty AttachProperty =
        DependencyProperty.RegisterAttached("Attach",
        typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach));

    private static readonly DependencyProperty IsUpdatingProperty =
       DependencyProperty.RegisterAttached("IsUpdating", typeof(bool), 
       typeof(PasswordHelper));


    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://wpftutorial.net/PasswordBox.html ".

You can refer the "http://wpftutorial.net/PasswordBox.html" for detail information.

这篇关于从XAML中定义的ListBoxItems的ListBox中的PasswordBox中获取密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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