MSDN > 論壇首頁 > Windows Presentation Foundation (WPF) > Unable to data-bind to a PasswordBox
發問發問
 

已答覆Unable to data-bind to a PasswordBox

  • 2005年10月28日 上午 01:30inuwan 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    When I attempt to bind to the Password property of the PasswordBox (binded to a property on an object that returns a string), I get the following error:

    Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.String'.

    Is string the wrong type....I tried char[] with the same result.

解答

  • 2005年11月9日 下午 05:43Ben Westbrook - MSFT 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    PasswordBox.Password doesn't have a corresponding DependencyProperty, so you can't data-bind to it.

    Exposing a DependencyProperty would require us to store the PasswordBox content plain text in memory in the property system -- which is a security concern.  The PasswordBox encrypts its content and only generates plain text on demand when a caller references the Password CLR property.

  • 2008年6月23日 下午 04:58LesterLobo - MSFT版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    As Ben mentioned, Databinding passwords is not a good design for security reasons and should be avoided.
  • 2008年6月29日 下午 04:32LesterLobo - MSFT版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    exposing a securestring password is something that we are looking at for the current release.

所有回覆

  • 2005年10月28日 上午 02:26Drew Marsh版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    You can't bind the property beause it is not a DependencyProperty. It is probably not a DP because Microsoft doesn't see any point in it being one and I'm inclined to share that perspective. In what scenario would you ever want to databind to a password box?

    HTH,
    Drew
  • 2005年10月28日 上午 05:15inuwan 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    I would like to bind to the Password box for the reason why you would want to data-bind anything...that is to enable the UI (XAML) to be less coupled to the application logic.  Without binding, I would have to know the name associated with the password box and then from code get a reference to the password box and directly access its Password property.  With binding, I simply know that there some data object bound to the page (or window, dialog, etc) and then get the password value from that object.
  • 2005年11月9日 下午 05:43Ben Westbrook - MSFT 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    PasswordBox.Password doesn't have a corresponding DependencyProperty, so you can't data-bind to it.

    Exposing a DependencyProperty would require us to store the PasswordBox content plain text in memory in the property system -- which is a security concern.  The PasswordBox encrypts its content and only generates plain text on demand when a caller references the Password CLR property.

  • 2006年12月6日 下午 05:04LeoXue 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    Hi,

      I want to use the Password of a PasswordBox as CommandParameter of a Button.

      Since the Password can't be dinding, how to do so?

     

  • 2008年6月23日 上午 09:51Samuel Jack 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    I've created a method to allow the Password on PasswordBox to be databound (using attached properties).

    You can get the code here: http://blog.functionalfun.net/2008/06/wpf-passwordbox-and-data-binding.html

  • 2008年6月23日 下午 04:58LesterLobo - MSFT版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    As Ben mentioned, Databinding passwords is not a good design for security reasons and should be avoided.
  • 2008年6月24日 上午 09:27Samuel Jack 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Lester,
       I agree that databinding passwords sometimes isn't a good idea, but I've posted my reasons why I don't think its always a problem in this post over here: http://forums.msdn.microsoft.com/en-US/wpf/thread/d5d9c146-3379-4a1e-8671-d37367ad4906.

    Could you explain further why you think it is always a bad idea? Also, is there a reason why you haven't exposed a SecureString-type property on PasswordBox, and why such a SecureString property couldn't be a dependency property?

    I'd really like to understand this.

    Thanks,

    Sam
  • 2008年6月29日 下午 04:32LesterLobo - MSFT版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    exposing a securestring password is something that we are looking at for the current release.
  • 2009年7月3日 下午 11:04Yossi Starz 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    So the reason we can't bind password is security?!
    What about Adding a dependency property at the view and we will do binding from the View Model to the DP.
    the DP will be filled with the password from the password box only when we need it (when we do login or any other command that we wanted to do) and we will clear the property right after that the View Model get the password and before any other function.

    That way we keep the view model and the view less coupled and we keep the security advantage of the Password Box.