4

我想将 User.Password 属性绑定到 PasswordBox (TwoWay)。由于 PasswordBox.Password 不可绑定,因此我制作了 AttachedProperties 来解决此问题(一个用于激活绑定,一个用于保存实际密码)。问题是它们不会绑定(GetBindingExpression 返回 null)。

还:

  • AttachedProperties 工作。如果我在 PasswordBox 中键入,Password 和 PasswordValue(附加属性)设置正确,但 User.Password 保持为空。
  • 绑定 AttachedProperty 也可以,但只能反过来:如果我将 PasswordValue 绑定到 TextBlock(TextBlock.Text 是目标,helper:PasswordValue 是源),它就可以工作。只有我不能使用它,因为 User 的属性不是依赖对象。
  • User.Password 是可绑定的(用户实现 INotifyPropertyChanged),我设法将 User.Username 绑定到 TextBox.Text 和(用户名和密码是相似的字符串属性)

以下是附加属性:

public static bool GetTurnOnBinding(DependencyObject obj)
        {
            return (bool)obj.GetValue(TurnOnBindingProperty);
        }

        public static void SetTurnOnBinding(DependencyObject obj, bool value)
        {
            obj.SetValue(TurnOnBindingProperty, value);
        }

        // Using a DependencyProperty as the backing store for TurnOnBinding. This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TurnOnBindingProperty =
            DependencyProperty.RegisterAttached(
            "TurnOnBinding",
            typeof(bool),
            typeof(PasswordHelper),
            new UIPropertyMetadata(false, (d, e) =>
            {
                var pb = d as PasswordBox;
                SetPasswordValue(pb, pb.Password);
                pb.PasswordChanged += (s, x) => SetPasswordValue(pb, pb.Password);
            }));

        public static string GetPasswordValue(DependencyObject obj)
        {
            return (string)obj.GetValue(PasswordValueProperty);
        }

        public static void SetPasswordValue(DependencyObject obj, string value)
        {
            obj.SetValue(PasswordValueProperty, value);
        }

        // Using a DependencyProperty as the backing store for PasswordValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PasswordValueProperty =
            DependencyProperty.RegisterAttached(
            "PasswordValue",
            typeof(string),
            typeof(PasswordHelper), new UIPropertyMetadata(null, (d, e) =>
            {
                PasswordBox p = d as PasswordBox;
                string s = e.NewValue as string;

                if (p.Password != s) p.Password = s;
            }));

以及带有绑定的 XAML 部分:

<PasswordBox x:Name="passBox" 
    root:PasswordHelper.TurnOnBinding="True" 
    root:PasswordHelper.PasswordValue="{Binding Text, 
        ElementName=passSupport, Mode=TwoWay}"/>
4

2 回答 2

2

很抱歉,我不能说您的代码有什么问题(恕我直言,它应该可以工作),但是由于我在 Silverlight 中遇到了类似的问题,而且设计者不太喜欢绑定到附加属性和从附加属性绑定,所以我发现他们不值得麻烦。
我目前扩展控件行为的首选方法是使用 System.Windows.Interactivity.Behavior 基类(参见此处此处),然后将我的行为附加到控件上。

因此,您需要一个class PasswordBehavior : Behavior<PasswordBox>,它覆盖OnAttached()并订阅 passwordBox 的 PasswordChanged 事件,并具有一个名为 PasswordValue 的可绑定 DependencyProperty。在事件处理程序中更改 dp 的值,并在 dp 的回调中更改控件的密码值。

于 2011-02-26T17:44:15.843 回答
0

查看PasswordBoxAssistant。只需更改属性 BoundPassword => BoundPasswordProperty, BindPassword => BindPasswordProperty, UpdatingPassword => UpdatingPasswordProperty 以避免 XAML 视图中的警告。

于 2011-02-26T01:56:56.160 回答