-3

I need to change my code to mvvm code. My scenario is as follows,If I select the checkbox Use password protection,both password and confirm password fields should be visible and If I select the checkbox show password ,both the password and confirm password should be visible as characters

How to write an Model.cs,view.xaml and viewmodel.cs for this scenario? Please help me... I have seen answers for similar questions but I didn't get clear with that.

2 Answers2

0

Add Visibility="Collapsed" or Visibility = "Visible" to the nodes you want to hide. Now add an event that switches those properties on click.

T-Me
  • 1,814
  • 1
  • 9
  • 22
  • That is fine.....that will come under the category code behind which I have already....I need to write it in mvvm(Model,View,ViewModel) pattern,for that what are all the changes I should do. – jayanthi govi Nov 27 '18 at 03:20
  • Can all be done in the View. Add in code behinde: `private void chkPasswordEnable_Checked(object sender, RoutedEventArgs e) { _GridNameHere_.Visibility = System.Windows.Visibility.Visible; }` – T-Me Nov 27 '18 at 07:46
  • There is an architecture called MVVM where we can add all the functionalities (like Onclick etc.,) inside ViewModel.cs intead of adding it in (View.xaml.cs) and link that to View.xaml....The benefit of using this property is that,incase in futher if we want change our windows application to ios application or mobile appliaction,we can modify only the user interface(i.e.) View.xaml, and don't need to change the ViewModel.cs where the functionalities of UI remains same. – jayanthi govi Nov 28 '18 at 04:40
  • You are right, thats why you are wrong: the code behind is part of the view and it should contain only code that is directly visual related. Whether your controls are visible or not is irrelevant to the viewmodel. But if you include it in the viewmodel, every future view will need those objects and the viewmodel will take the job of changing the visual apearance. (You could make a view with 100 buttons and 90 of them are only hiding or showing each other > not the business of the viewmodel) – T-Me Nov 28 '18 at 11:39
-1

Create a ViewModel class which implements INotifyPropertyChanged. Create a boolean property called UsePasswordProtection which raises PropertyChanged event if the value changes. The view's checkbox for UsePasswordProtection has a property which indicates whether it is checked. In the XAML for that property's value, set it to be a binding to the ViewModel UsePasswordProtection property.

In the view, both the password and confirm boxes' Visibility properties should be bound to the ViewModel UsePasswordProtection property, using the BooleanToVisibilityConverter as the converter.

I would argue that the ViewModel doesn't need to be involved in the "Show password" functionality, since that is a cosmetic, UI-only thing (unless any part of your application cares about the value) so it's one of the few things I would do in code behind. Here's a little trick to achieve that... showing password characters on some event for passwordbox

Richardissimo
  • 5,596
  • 2
  • 18
  • 36