سؤال

مرحبا الناس stackoverflow. أنا أعمل مع MVVM، لدي ViewModel. اتصل userviewmodel مع كلمة مرور العقارات. في ال رأي لديك علبة مرور التحكم.

<PasswordBox x:Name="txtPassword" Password="{Binding Password}" />

ولكن هذا xaml لا تعمل. كيف يمكنك أن تربط؟ ساعدنى من فضلك!!

هل كانت مفيدة؟

المحلول

لأسباب أمنية، فإن خاصية كلمة المرور ليست خاصية التبعية وبالتالي لا يمكنك ربطها بذلك. لسوء الحظ، ستحتاج إلى أداء الربط في التعليمات البرمجية خلف الطريقة القديمة (التسجيل لحدث OnPropertychanged وتحديث القيمة من خلال التعليمات البرمجية ...)


أنا البحث السريع يجلبني إلى هذه المدونة post. الذي يوضح كيفية كتابة خاصية مرفقة إلى SIDESTEP المشكلة. ما إذا كان هذا يستحق القيام أو لا يعتمد حقا على النفور الخاص بك من الرمز وراء.

نصائح أخرى

يمكنك دائما كتابة عنصر تحكم يلتف كلمة المرور ويضيف خاصية التبعية الخاصة بخاصية كلمة المرور.

أود فقط استخدام التعليمات البرمجية خلفها، ولكن إذا كان يجب عليك القيام بشيء ما:

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;
    }

}

هناك مشكلة مع bindablePasswordboard. يعمل فقط في اتجاه واحد، علبة مرور كلمة المرور إلى passwordproperty. فيما يلي نسخة معدلة منها تعمل في كلا الاتجاهين. يسجل 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;
}

في حين أنه من المنطقي توفير كلمة SecurePass مباشرة من الربط، يبدو دائما أنه يوفر قيمة فارغة. لذلك هذا لا يعمل:

    <Button Margin="0 0 8 0" MinWidth="65" 
            Command="{Binding LoginAccept}" 
            CommandParameter="{Binding ElementName=PasswordBox, Path=SecurePassword}">

تحقق من ترابط آخر على مربع كلمة المرور. من الأفضل عدم الحفاظ على كلمة المرور على أي موانئ دبي أو الممتلكات العامة.

موضوع آخر على passwordbox

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top