0

I'm developing a Windows 8.1 Store app with C# and .Net Framework 4.5.1.

I'm trying to bind Password.SecurePassword to a ViewModel, and reading this SO answer I found a way to do it: Put the PasswordBox in my ViewModel.

But I don't know how to do it. I know how to bind Dependency Properties, but I don't know how to put that control on my ViewModel. This is my XAML:

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    DataContext="{Binding MainViewModel, Source={StaticResource Locator}}"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <PasswordBox x:Name="userPassword" />
    </Grid>
</Page>

What do I have to do?

Community
  • 1
  • 1
VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • The post you link to describers the specific problem for `PassWord` (it's not a DP) and a solution. It's not going to be easier than that. – H H Oct 05 '14 at 09:06
  • maybe that post will help http://stackoverflow.com/questions/1483892/how-to-bind-to-a-passwordbox-in-mvvm – Roman G. Oct 05 '14 at 09:41

1 Answers1

0

You have several options but I'll just give you the basic option without third party libraries.

In your Page constructor. You can do something like this.

public Page()
{
  var mainViewModel = this.DataContext as MainViewModel;
  if(mainViewModel != null)
  {
    mainViewModel.PasswordBox = userPassword;
  }
}

You can also set it on the Loaded event of the View and set the PasswordBox to the ViewModel.

123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
  • Thanks for your answer. Here, http://stackoverflow.com/a/4649830/68571, I have found how to do it. Thanks. – VansFannel Oct 06 '14 at 08:04
  • @VansFannel If you're gonna use `CommandParameters` just to pass the `PasswordBox` to the ViewModel. I'll just use `Behaviors`'s on Attach/Dettach to pass it to the `ViewModel`. I find it more cleaner and readable. – 123 456 789 0 Oct 06 '14 at 21:03