37058

Put a View Control in its ViewModel

Question:

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 <a href="https://stackoverflow.com/a/23020395/68571" rel="nofollow">SO answer</a> 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?

Answer1:

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 <strong>ViewModel</strong>.

Recommend

  • WPF8/C# - Binding Data To Grid
  • Windows 8 Metro style ListView auto scroll
  • UWP - Show textbox text highlight while textbox is out of focus
  • Detaching entity along with referenced entities
  • Powerpoint 2010 VSTO AddIn taskpane on multiple Windows
  • How to clip image to ellipse in XAML
  • Keep transition effect on 1 div from moving the div that follows?
  • Updating one element of a bound Observable collection
  • How to force Composer to download a local package?
  • How to convert WPF project so it can be used as a class library by a separate exe
  • Android Lock Screen C# .NET Replica
  • How do you create a Fuseki SPARQL server using the Apache Jena Java API?
  • Splash Screen will not display
  • WP8.1 AppBarButton holding event
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • JBoss External Properties Files in Classpath
  • gspread or such: help me get cell coordinates (not value)
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Java: can you cast Class into a specific interface?
  • Q promise. Difference between .when and .then
  • Control modification in presentation layer
  • Pass value from viewmodel to script in zk
  • Nant, Vault & Windows Integrated Authentication
  • Bug in WPF DataGrid
  • AES padding and writing the ciphertext to a disk file
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • jQuery tmpl and DataLink beta
  • Web-crawler for facebook in python
  • How can I estimate amount of memory left with calling System.gc()?
  • A cron job substitute?
  • C# - Getting references of reference
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal