没有setter的属性如何进行数据绑定

5

我该如何在WPF中将数据绑定到仅有getter而没有setter的属性,并从视图模型中访问它?我正在使用 PasswordBox 并希望将其 SecureString 属性绑定到ViewModel属性。我该怎么做?


你想要绑定到视图模型上的只读属性吗? - Ibrahim Najjar
我对WPF和MVVM都很陌生。我想在我的ViewModel中获取密码的SecureString,但不会进行设置。 - Victor Mukherjee
你的问题在于PasswordBox控件上的SecureString属性是只读属性,这意味着你无法从标记或代码中设置它。 - Ibrahim Najjar
你想让PasswordBox从XAML设置一个类型为SecureString的属性到你的视图模型吗? - Ibrahim Najjar
2
你读过这个吗? - Vyacheslav Volkov
是的,那正是我要找的。我想在我的视图模型中检索用户输入的密码。 - Victor Mukherjee
3个回答

2
我使用这个类以及 System.Windows.Interactivity 库来访问没有 setter 的属性:

并且我使用了 System.Windows.Interactivity 库。

public sealed class PropertyManager : TriggerAction<FrameworkElement>
{
    #region Fields

    private bool _bindingUpdating;
    private PropertyInfo _currentProperty;
    private bool _propertyUpdating;

    #endregion

    #region Dependency properties

    /// <summary>
    ///     Identifies the <see cref="Binding" /> dependency property.
    /// </summary>
    public static readonly DependencyProperty BindingProperty =
        DependencyProperty.Register("Binding", typeof(object), typeof(PropertyManager),
            new PropertyMetadata((o, args) =>
            {
                var propertyManager = o as PropertyManager;
                if (propertyManager == null ||
                    args.OldValue == args.NewValue) return;
                propertyManager.TrySetProperty(args.NewValue);
            }));

    /// <summary>
    ///     Identifies the <see cref="SourceProperty" /> dependency property.
    /// </summary>
    public static readonly DependencyProperty SourcePropertyProperty =
        DependencyProperty.Register("SourceProperty", typeof(string), typeof(PropertyManager),
            new PropertyMetadata(default(string)));

    /// <summary>
    ///     Binding for property <see cref="SourceProperty" />.
    /// </summary>
    public object Binding
    {
        get { return GetValue(BindingProperty); }
        set { SetValue(BindingProperty, value); }
    }

    /// <summary>
    ///     Name property to bind.
    /// </summary>
    public string SourceProperty
    {
        get { return (string)GetValue(SourcePropertyProperty); }
        set { SetValue(SourcePropertyProperty, value); }
    }

    #endregion

    #region Methods

    /// <summary>
    ///     Invokes the action.
    /// </summary>
    /// <param name="parameter">
    ///     The parameter to the action. If the action does not require a parameter, the parameter may be
    ///     set to a null reference.
    /// </param>
    protected override void Invoke(object parameter)
    {
        TrySetBinding();
    }

    /// <summary>
    ///     Tries to set binding value.
    /// </summary>
    private void TrySetBinding()
    {
        if (_propertyUpdating) return;
        PropertyInfo propertyInfo = GetPropertyInfo();
        if (propertyInfo == null) return;
        if (!propertyInfo.CanRead)
            return;
        _bindingUpdating = true;
        try
        {
            Binding = propertyInfo.GetValue(AssociatedObject, null);
        }
        finally
        {
            _bindingUpdating = false;
        }
    }

    /// <summary>
    ///     Tries to set property value.
    /// </summary>
    private void TrySetProperty(object value)
    {
        if (_bindingUpdating) return;
        PropertyInfo propertyInfo = GetPropertyInfo();
        if (propertyInfo == null) return;
        if (!propertyInfo.CanWrite)
            return;
        _propertyUpdating = true;
        try
        {
            propertyInfo.SetValue(AssociatedObject, value, null);
        }
        finally
        {
            _propertyUpdating = false;
        }
    }

    private PropertyInfo GetPropertyInfo()
    {
        if (_currentProperty != null && _currentProperty.Name == SourceProperty)
            return _currentProperty;
        if (AssociatedObject == null)
            throw new NullReferenceException("AssociatedObject is null.");
        if (string.IsNullOrEmpty(SourceProperty))
            throw new NullReferenceException("SourceProperty is null.");
        _currentProperty = AssociatedObject
            .GetType()
            .GetProperty(SourceProperty);
        if (_currentProperty == null)
            throw new NullReferenceException("Property not found in associated object, property name: " +
                                                SourceProperty);
        return _currentProperty;
    }

    #endregion
}

要在XAML中使用此类,您需要添加对System.Windows.Interactivity库的引用并添加以下命名空间:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:behaviors="clr-namespace:YOUR NAMESPACE WHERE YOU PUT THE PropertyManager CLASS"

您还需要指定要更新值的事件,在本例中为PasswordChanged,并指定您要绑定的属性,在本例中为Password

<PasswordBox>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="PasswordChanged">
            <behaviors:PropertyManager
                Binding="{Binding Path=MyPasswordProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                SourceProperty="Password" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</PasswordBox>

这个类功能强大,可以与任何属性一起使用,并支持双向绑定。


太棒了!我不是在用这个来做密码框,但@Victor Mukherjee问了一个非常好的问题,而你的回答也很好。 - Raiden Core
我有点儿嫉妒,谁写了PropertyManager类? - Raiden Core

0

XAML中的绑定:

<PasswordBox Text="{Binding SecureString, Mode=OneWay}"...

如果你不想从XAML绑定中更改它

public string SecureString
{
   get { return _secureString;}
   private set
   {
      if(_secureString == value) return;
      _secureString = value;
      RaisePropertyChanged(() => SecureString);
   }

 public void SetSecureString(string newSecureString)
 {
     SecureString = newSecureString;
 }

你的 ViewModel 的使用者应该可以通过那个方法设置 SecureString。

0

您可以通过将绑定模式设置为OneWay来绑定只读属性 -

<PasswordBox Text="{Binding SecureString, Mode=OneWay}"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接