简体   繁体   English

WPF PasswordBox密码为“ {DependencyProperty.UnsetValue}”

[英]WPF PasswordBox Password is “{DependencyProperty.UnsetValue}”

I'm making a chating program. 我正在编写一个聊天程序。

I designed chat room list using XAML. 我使用XAML设计了聊天室列表。

                                        <GridViewColumn x:Name="gridViewColumn_IsNeedPassword">
                                            <GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <PasswordBox x:Name="passwordBox_PW" MinWidth="100" IsEnabled="{Binding Path=IsNeedPassword}"/>
                                                </DataTemplate>
                                            </GridViewColumn.CellTemplate>
                                        </GridViewColumn>
                                        <GridViewColumn x:Name="gridViewColumn_EntryButton">
                                            <GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <Button Background="Aqua" Click="button_Entry_Click">
                                                        <StackPanel Orientation="Horizontal">
                                                            <Image Height="Auto" Width="Auto" Source="Resources/login.png"/>
                                                            <TextBlock Text="{Binding Converter={StaticResource EntryButtonConverter}}" VerticalAlignment="Center"/>
                                                        </StackPanel>
                                                        <Button.Tag>
                                                            <MultiBinding Converter="{StaticResource EntryButtonTagConverter}">
                                                                <Binding Path="ID"/>
                                                                <Binding Path="IsNeedPassword"/>
                                                                <Binding ElementName="passwordBox_PW" Path="Password"/>
                                                            </MultiBinding>
                                                        </Button.Tag>
                                                    </Button>
                                                </DataTemplate>
                                            </GridViewColumn.CellTemplate>
                                        </GridViewColumn>
                                        <GridViewColumn x:Name="gridViewColumn_DeleteButton">
                                            <GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <Button Background="Orange" Click="button_Delete_Click" IsEnabled="{Binding Path=Master, Converter={StaticResource DeleteButtonVisibilityConverter}}">
                                                        <StackPanel Orientation="Horizontal">
                                                            <Image Height="Auto" Width="Auto" Source="Resources/login.png"/>
                                                            <TextBlock Text="{Binding Converter={StaticResource DeleteButtonConverter}}" VerticalAlignment="Center"/>
                                                        </StackPanel>
                                                        <Button.Tag>
                                                            <Binding Path="ID"/>
                                                        </Button.Tag>
                                                    </Button>
                                                </DataTemplate>
                                            </GridViewColumn.CellTemplate>
                                        </GridViewColumn>
                                    </GridView.Columns>

Something like this. 这样的事情。

Now, in the gridViewColumn_EntryButton I need some infos such as RoomID + IsNeedPassword + PasswordText 现在,在gridViewColumn_EntryButton我需要一些相关信息如RoomID + IsNeedPassword + PasswordText

So i used MultiBinding . 所以我用了MultiBinding

and the EntryButtonTagConverter.Convert is like that. EntryButtonTagConverter.Convert就是这样。

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        string[] result = Array.ConvertAll<object, string>(values, obj =>
        {
            return (obj == null) ? string.Empty : obj.ToString();
        });
        // RoomID + IsNeedPassword + PasswordText
        return result[0] + '\n' + result[1] + '\n' + result[2];
    }

and When i debugging, the result[2] , PasswordText is "{DependencyProperty.UnsetValue}" 当我调试时, result[2]PasswordText"{DependencyProperty.UnsetValue}"

But i inputed into the PasswordBox asdftest1234 . 但我输入了PasswordBox asdftest1234

I don't know why PasswordBox.Password property is not accessable. 我不知道为什么不能访问PasswordBox.Password属性。

Any one some ideas? 有任何想法吗?

Thanks. 谢谢。

It´s not possible to bind directly to the PasswordProperty for security reasons. 出于安全原因,不可能直接绑定到PasswordProperty。

Take a look here! 在这里看看

Using PasswordBoxAssistant you can bind password. 使用PasswordBoxAssistant可以绑定密码。

public static class PasswordBoxAssistant
{
    public static readonly DependencyProperty BoundPasswordProperty =
        DependencyProperty.RegisterAttached("BoundPassword", typeof(string)
        , typeof(PasswordBoxAssistant), new FrameworkPropertyMetadata(string.Empty, OnBoundPasswordChanged));

    public static readonly DependencyProperty BindPasswordProperty = DependencyProperty.RegisterAttached(
        "BindPassword", typeof(bool), typeof(PasswordBoxAssistant), new PropertyMetadata(false, OnBindPasswordChanged));

    private static readonly DependencyProperty UpdatingPasswordProperty =
        DependencyProperty.RegisterAttached("UpdatingPassword", typeof(bool), typeof(PasswordBoxAssistant));

    private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        PasswordBox box = d as PasswordBox;

        // only handle this event when the property is attached to a PasswordBox  
        // and when the BindPassword attached property has been set to true  
        var ignoreBindProperty = false;
        if (box != null && box.Parent != null)
        {// TODO: Bind property change not set in case of hosting password box under Telerik datafield. That why I am ignoring the bind propery here - Morshed
            ignoreBindProperty = (box.Parent is Telerik.Windows.Controls.DataFormDataField);
        }

        if (d == null || !(GetBindPassword(d) || ignoreBindProperty))
        {
            return;
        }

        // avoid recursive updating by ignoring the box's changed event  
        box.PasswordChanged -= HandlePasswordChanged;

        string newPassword = (string)e.NewValue;

        if (!GetUpdatingPassword(box))
        {
            box.Password = newPassword;
        }

        box.PasswordChanged += HandlePasswordChanged;
    }

    private static void OnBindPasswordChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
    {
        // when the BindPassword attached property is set on a PasswordBox,  
        // start listening to its PasswordChanged event  

        PasswordBox box = dp as PasswordBox;

        if (box == null)
        {
            return;
        }

        bool wasBound = (bool)(e.OldValue);
        bool needToBind = (bool)(e.NewValue);

        if (wasBound)
        {
            box.PasswordChanged -= HandlePasswordChanged;
        }

        if (needToBind)
        {
            box.PasswordChanged += HandlePasswordChanged;
        }
    }

    private static void HandlePasswordChanged(object sender, RoutedEventArgs e)
    {
        PasswordBox box = sender as PasswordBox;

        // set a flag to indicate that we're updating the password  
        SetUpdatingPassword(box, true);
        // push the new password into the BoundPassword property  
        SetBoundPassword(box, box.Password);
        SetUpdatingPassword(box, false);
    }

    public static void SetBindPassword(DependencyObject dp, bool value)
    {
        dp.SetValue(BindPasswordProperty, value);
    }

    public static bool GetBindPassword(DependencyObject dp)
    {
        return (bool)dp.GetValue(BindPasswordProperty);
    }

    public static string GetBoundPassword(DependencyObject dp)
    {
        return (string)dp.GetValue(BoundPasswordProperty);
    }

    public static void SetBoundPassword(DependencyObject dp, string value)
    {
        dp.SetValue(BoundPasswordProperty, value);
    }

    private static bool GetUpdatingPassword(DependencyObject dp)
    {
        return (bool)dp.GetValue(UpdatingPasswordProperty);
    }

    private static void SetUpdatingPassword(DependencyObject dp, bool value)
    {
        dp.SetValue(UpdatingPasswordProperty, value);
    }
}

and change in xaml 并更改XAML

<PasswordBox helpers:PasswordBoxAssistant.BindPassword="true"
              helpers:PasswordBoxAssistant.BoundPassword="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true}"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF 转换器中的多重绑定失败 ==&gt; DependencyProperty.UnsetValue - WPF MultiBinding in Convertor fails ==> DependencyProperty.UnsetValue 多重绑定中的DependencyProperty.unsetValue - DependencyProperty.unsetValue in a multibinding WPF标签内容在设计模式下显示DependencyProperty.UnsetValue - WPF Label content shows DependencyProperty.UnsetValue in design mode 绑定到CollectionViewSource返回DependencyProperty.UnsetValue - Binding to a CollectionViewSource returns DependencyProperty.UnsetValue 为什么DependencyProperty.UnsetValue传递给Equals() - Why DependencyProperty.UnsetValue is being passed into Equals() IMul​​tiValueConverter始终将DependencyProperty.UnsetValue传递给列表 - IMultiValueConverter always passes in DependencyProperty.UnsetValue for list 多绑定时的随机 DependencyProperty.UnsetValue - Random DependencyProperty.UnsetValue when multibinding 字典比可以返回DependencyProperty.UnsetValue - Dictionary than can return DependencyProperty.UnsetValue 静态资源错误:“{DependencyProperty.UnsetValue}”不是属性的有效值 - StaticResource error : `{DependencyProperty.UnsetValue}' is not a valid value for property 将 RichTextBox 的 SelectionChanged 事件和 FontSize 的组合框的 TextChanged 事件一起使用会引发 DependencyProperty.UnsetValue 错误 - Using together SelectionChanged event of RichTextBox and TextChanged event of of a comboBox for FontSize throws DependencyProperty.UnsetValue error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM