【问题标题】:Password/Mask field in WPF PropertyGrid is not persistedWPF PropertyGrid 中的密码/掩码字段未保留
【发布时间】:2016-11-01 01:27:33
【问题描述】:

我正在尝试为 WF 重新托管设计器解决方案中使用的 WPF PropertyGrid 构建密码(屏蔽)字段。请注意,我对它的安全元素不感兴趣。我只想对用户隐藏密码。我真的为一些我认为很容易实现的东西而苦苦挣扎,但我最终发现这篇很棒的文章真的很有帮助:

WPF PasswordBox and Data binding

根据文章创建 PasswordBoxAssistant 类后,我使用以下 XAML 和代码创建了 WPF UserControl:

XAML

<Grid>
    <PasswordBox x:Name="PasswordBox"
                 Background="Green" 
                 local:PasswordBoxAssistant.BindPassword="true" 
                 local:PasswordBoxAssistant.BoundPassword="{Binding Password,
                 Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>

代码隐藏:

public partial class PasswordUserControl : UserControl
{
    public PasswordUserControl()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty PasswordProperty = 
      DependencyProperty.Register("Password",
      typeof(string), typeof(PasswordUserControl),
      new FrameworkPropertyMetadata(PasswordChanged));

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

    private static void PasswordChanged(DependencyObject source, 
       DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue != null)
        {
            (source as PasswordUserControl)?.UpdatePassword(e.NewValue.ToString());
        }
        else
        {
            (source as PasswordUserControl)?.UpdatePassword(string.Empty);
        }
    }

    private void UpdatePassword(string newText)
    {
        PasswordBox.Password = Password;
    }
}

然后我创建了一个PropertyValueEditor 类(称为PasswordEditor.cs),我假设这是不工作的部分,我没有正确设置。

我将 PasswordProperty 从 UserControl 绑定到 PropertyValueEditorValue 字段。

public class PasswordEditor : PropertyValueEditor
{
    public PasswordEditor()
    {
        this.InlineEditorTemplate = new DataTemplate();
        FrameworkElementFactory stack = new FrameworkElementFactory(typeof(StackPanel));
        FrameworkElementFactory passwordBox = new 
                    FrameworkElementFactory(typeof(PasswordUserControl));
        Binding passwordBoxBinding = new Binding("Value") { Mode = BindingMode.TwoWay };
        passwordBox.SetValue(PasswordUserControl.PasswordProperty, passwordBoxBinding);
        stack.AppendChild(passwordBox);
        this.InlineEditorTemplate.VisualTree = stack;
    }
}

我已尝试将其设置为 StringValue 以及我发现的其他 WF PropertyValueEditor 示例,但无济于事。

密码(屏蔽)字段现在在我的 WPF PropertyGrid 中正确显示,但当我切换到另一个 WF 活动并切换回包含密码字段的活动时,它不会保留该值。

谁能指出我正确的方向?

谢谢。

再次感谢任何帮助。

谢谢。

【问题讨论】:

    标签: c# wpf data-binding workflow-foundation-4 propertygrid


    【解决方案1】:

    我终于明白了!!

    解决持久性问题:

    我更改的第一件事是在XAML 代码中,我将绑定的Password 属性更改为Value

    <Grid>
        <PasswordBox x:Name="PasswordBox"
                     Background="Green" 
                     local:PasswordBoxAssistant.BindPassword="true" 
                     local:PasswordBoxAssistant.BoundPassword="{Binding Value, Mode=TwoWay, 
                     UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
    

    我更改的第二件事是在 PasswordEditor 中调用 SetValue 而不是 SetBinding,因此将其更改为:

    passwordBox.SetBinding(PasswordUserControl.PasswordProperty, 
                           passwordBoxBinding);
    

    现在剩下的奇怪问题是,当我在 PasswordUserControl 中包含的 PasswordBox 中键入字符时,它总是将光标放在第一个位置,a) 真的很奇怪,看起来不对,b) 它导致密码向后,所以如果我输入“Hello”,它将是“olleH”。

    我知道要解决它并继续前进,我每次都可以简单地反转字符串,但是让光标停留在文本框的前面对我来说太奇怪了,所以我真的很想弄清楚找出是什么原因造成的。

    解决光标位置和反串问题:

    我更改了 PasswordUserControl 中的PasswordChanged 事件:

        private static void PasswordChanged(DependencyObject source,
          DependencyPropertyChangedEventArgs e)
        {
            var passwordUserControl = source as PasswordUserControl;
    
            if (passwordUserControl != null)
                if (e.NewValue != null) passwordUserControl.Password =
                    e.NewValue.ToString();
        }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2011-10-10
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      相关资源
      最近更新 更多