我有一个WPF应用程序,我正在使用MVVM模式实现。在这个应用程序中,我试图通过附加属性从PasswordBox获取密码。但是,我收到上面提到的错误,我不知道为什么我会得到它。这就是我所拥有的:
XAML
<PasswordBox x:Name="passwordTextbox" HorizontalAlignment="Left" Height="31" Margin="316,194,0,0" VerticalAlignment="Top" Width="208"
FontSize="16" IsEnabled="{Binding IsEnabled}"
vm:PasswordBoxAttachedProperty.EncryptedPassword="PasswordSecureString, Mode=Twoway, UpdateSourceTrigger=PropertyChanged" />
附加财产
using System.Security;
using System.Windows;
namespace QMAC.ViewModel
{
public static class PasswordBoxAttachedProperty
{
public static SecureString GetEncryptedPassword(DependencyObject obj)
{
return (SecureString)obj.GetValue(EncryptedPasswordProperty);
}
public static void SetEncryptedPassword(DependencyObject obj, SecureString value)
{
obj.SetValue(EncryptedPasswordProperty, value);
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty EncryptedPasswordProperty =
DependencyProperty.RegisterAttached("EncryptedPassword", typeof(SecureString), typeof(PasswordBoxAttachedProperty));
}
}
有什么建议吗?
答案 0 :(得分:3)
你可能想写
vm:PasswordBoxAttachedProperty.EncryptedPassword=
"{Binding PasswordSecureString, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}"
而不是
vm:PasswordBoxAttachedProperty.EncryptedPassword=
"PasswordSecureString, Mode=Twoway, UpdateSourceTrigger=PropertyChanged"