【问题标题】:How to bind to attached property in XAML, WPF如何绑定到 XAML、WPF 中的附加属性
【发布时间】:2011-07-03 21:44:53
【问题描述】:

我想将 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 和(用户名和密码是相似的字符串属性)

这里是 AttachedProperties:

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}"/>

【问题讨论】:

    标签: wpf data-binding binding attached-properties


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2020-12-22
        • 1970-01-01
        • 2023-01-16
        • 1970-01-01
        • 2022-01-14
        • 1970-01-01
        相关资源
        最近更新 更多