문제
안녕하세요 사람들은 stackoverflow입니다. 나는 MVVM과 함께 일하고 있습니다 뷰 모델 속성 암호로 userviewModel을 호출하십시오. 에서 보다 제어 비밀번호 상자가 있습니다.
<PasswordBox x:Name="txtPassword" Password="{Binding Password}" />
그러나이 xaml은 작동하지 않습니다. 바인딩을 어떻게합니까 ?? 도와주세요!!
해결책
보안상의 이유로 암호 속성은 종속성 속성이 아니므로 구속력이 없습니다. 불행히도 구식 길 뒤의 코드에서 바인딩을 수행해야합니다 (OnPropertyChanged 이벤트에 등록하고 코드를 통해 값을 업데이트합니다 ...)
나는 빠른 검색을 통해 나를 데려옵니다 이 블로그 게시물 문제를 회피하기 위해 첨부 된 속성을 작성하는 방법을 보여줍니다. 이것이 가치가 있는지 여부는 실제로 코드-비만에 대한 혐오에 달려 있습니다.
다른 팁
항상 비밀번호를 감싸고 암호 속성에 대한 종속성 속성을 추가하는 컨트롤을 작성할 수 있습니다.
나는 단지 코드를 사용하지만, 당신이해야한다면 다음과 같은 것을 할 수 있습니다.
public class BindablePasswordBox : Decorator
{
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.Register("Password", typeof(string), typeof(BindablePasswordBox));
public string Password
{
get { return (string)GetValue(PasswordProperty); }
set { SetValue(PasswordProperty, value); }
}
public BindablePasswordBox()
{
Child = new PasswordBox();
((PasswordBox)Child).PasswordChanged += BindablePasswordBox_PasswordChanged;
}
void BindablePasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
{
Password = ((PasswordBox)Child).Password;
}
}
BindablePasswordbox에는 문제가 있습니다. 한 방향으로 만 작동합니다. 아래는 양방향으로 작동하는 수정 된 버전입니다. PropertyChangedCallback을 등록하고 호출되면 PasswordBox의 비밀번호를 업데이트합니다. 나는 누군가가 이것을 유용하다는 것을 알기를 바랍니다.
public class BindablePasswordBox : Decorator
{
public static readonly DependencyProperty PasswordProperty = DependencyProperty.Register("Password", typeof(string), typeof(BindablePasswordBox), new PropertyMetadata(string.Empty, OnDependencyPropertyChanged));
public string Password
{
get { return (string)GetValue(PasswordProperty); }
set { SetValue(PasswordProperty, value); }
}
private static void OnDependencyPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
BindablePasswordBox p = source as BindablePasswordBox;
if (p != null)
{
if (e.Property == PasswordProperty)
{
var pb = p.Child as PasswordBox;
if (pb != null)
{
if (pb.Password != p.Password)
pb.Password = p.Password;
}
}
}
}
public BindablePasswordBox()
{
Child = new PasswordBox();
((PasswordBox)Child).PasswordChanged += BindablePasswordBox_PasswordChanged;
}
void BindablePasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
{
Password = ((PasswordBox)Child).Password;
}
}
언제라도 메모리에서 비밀번호를 메모리로 사용할 수있게하려면 내 명령의 매개 변수로 값을 제공합니다.
<Label>User Name</Label>
<TextBox Text="{Binding UserName}" />
<Label>Password</Label>
<PasswordBox Name="PasswordBox" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0 16 0 0">
<Button Margin="0 0 8 0" MinWidth="65"
Command="{Binding LoginAccept}"
CommandParameter="{Binding ElementName=PasswordBox}">
Login
</Button>
<Button MinWidth="65" Command="{Binding LoginCancel}">Cancel</Button>
</StackPanel>
그런 다음 내보기 모델에서.
public DelegateCommand<object> LoginAccept { get; private set; }
public DelegateCommand<object> LoginCancel { get; private set; }
public LoginViewModel {
LoginAccept = new DelegateCommand<object>(o => OnLogin(o), (o) => IsLoginVisible);
LoginCancel = new DelegateCommand<object>(o => OnLoginCancel(), (o) => IsLoginVisible);
}
private void OnLogin(object o)
{
var passwordBox = (o as System.Windows.Controls.PasswordBox);
var password = passwordBox.SecurePassword.Copy();
passwordBox.Clear();
ShowLogin = false;
var credential = new System.Net.NetworkCredential(UserName, password);
}
private void OnLoginCancel()
{
ShowLogin = false;
}
바인딩에서 SecurePassword를 직접 제공하는 것이 합리적이지만 항상 빈 값을 제공하는 것 같습니다. 그래서 이것은 작동하지 않습니다.
<Button Margin="0 0 8 0" MinWidth="65"
Command="{Binding LoginAccept}"
CommandParameter="{Binding ElementName=PasswordBox, Path=SecurePassword}">
비밀번호 상자에서 다른 스레드를 확인하십시오. DP 또는 공공 자산에 비밀번호를 유지하지 않는 것이 좋습니다.