【问题标题】:Get Password from PasswordBox in ListBox of ListBoxItems defined in XAML从 XAML 中定义的 ListBoxItems 的 ListBox 中的 PasswordBox 获取密码
【发布时间】:2013-12-02 07:10:28
【问题描述】:

我有一个WPFListBox,每个ListBoxItem 有很多UIElements。其中UIElements 之一是PasswordBox。这些UIElementsXaml ControlTemplate 中定义。

我需要做的是在后面的代码中从PasswordBox 获取一个Password 字符串。但是我无法绑定PasswordBoxPassword 属性,这不是动态属性,因为Password 不应该长时间存储在内存中。

现在我想到的解决方案是在后面的代码中获取ListBoxItem 中的UIElements,然后从那里获取Password

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

【问题讨论】:

    标签: c# .net wpf xaml listbox


    【解决方案1】:

    密码框的密码属性不是依赖属性,所以不能绑定该属性。

    您可以通过以下任一方式从密码框控件中获取密码。

    1. 将密码框控件作为参数传递给项目附加的命令。

      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 模式,因为现在视图模型知道视图是如何实现的。

    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”获取详细信息。

    【讨论】:

    • 使用第一个解决方案,我知道它违反了 MVVM,但我希望它更安全。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2013-02-01
    • 2014-04-26
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    相关资源
    最近更新 更多