How To Display And Hide A Control In MVVM Pattern Using Prism Library In WPF

If you are the new in MVVM pattern using Prism Library then you can follow my very first article to start the MVVM and add the dlls into the project from the following link:

Getting Started: MVVM Pattern Using Prism Library in WPF

Now I will show you a demo to make visible and hide a label via the click event of a button from the view in view model.

Note: In this article I am using Visual Studio 2013 and ‘Prism.Unity’ via nugget Packages.

Step 1:- Create a project named ‘PrismMVVMTestProject’ of WPF application.

WPF

Step 2:- It’s a better approach to create the 3 different folders in the project for Model, View and View model respectively.

model

Step 3:- Create pages in all folders

i. Create a View Named ‘TestView.xaml’ in the Views folder.

View

ii. Create a Model Named ‘TestModel.cs’ in the Models folder.

Model

iii. Create a ViewModel Named ‘TestViewModel.cs’ in the ViewModels folder.

ViewModels

Step 4:

Add the namespace named ‘Prism.MVVM’ in the TestModel page to inherit the class named ‘Bindable Base’ and ‘System.Windows’ for ‘Visibility’. Create a property named Message where ‘ref‘ parameter allows you to update its value

  1. using Prism.Mvvm;  
  2. using System.Windows;  
  3. namespace PrismMVVMTestProject.Models  
  4. {  
  5.     class TestModel : BindableBase  
  6.     {  
  7.   
  8.         private Visibility _MessageVisibilty;  
  9.         public Visibility MessageVisibilty { get { return _MessageVisibilty; } set { SetProperty(ref _MessageVisibilty, value); } }  
  10.     }  
  11. }  

code

Step 5:-

a. Add the below namespaces on the TestViewModel page,

  • ‘Prism.MVVM’ - To inherit the class named ‘Bindable Base’
  • PrismMVVMTestProject.Models’ - To accessing TestModel in this page.
  • System.Windows.Input - To add ICommand
  • Prism.Commands - To Use DelegateCommand
  • ‘System.Windows’ for ‘Visibility’.

b. Create a property of TestModel class object where ‘ref‘ parameter allows you to update its value.

c. Attach 2 commands to the both methods which will act like event for visible and hide the label control.

d. Show the label in Show Method and Hide the label in HideMethod.

  1. using PrismMVVMTestProject.Models;  
  2. using Prism.Mvvm;  
  3. using System.Windows;  
  4. using System.Windows.Input;  
  5. using Prism.Commands;  
  6.   
  7. namespace PrismMVVMTestProject.ViewModels  
  8. {  
  9.     class TestViewModel : BindableBase  
  10.     {  
  11.         private TestModel testModel;  
  12.         public ICommand ShowCommand { getprivate set; }  
  13.         public ICommand HideCommand { getprivate set; }  
  14.   
  15.         public TestViewModel()  
  16.         {  
  17.             testModel = new TestModel();  
  18.             ShowCommand = new DelegateCommand(ShowMethod);  
  19.             HideCommand = new DelegateCommand(HideMethod);  
  20.         }  
  21.   
  22.         public TestModel TestModel  
  23.         {  
  24.             get { return testModel; }  
  25.             set { SetProperty(ref testModel, value); }  
  26.         }  
  27.         private void ShowMethod()  
  28.         {  
  29.             TestModel.MessageVisibilty = Visibility.Visible;  
  30.         }  
  31.         private void HideMethod()  
  32.         {  
  33.             TestModel.MessageVisibilty = Visibility.Hidden;  
  34.         }  
  35.   
  36.     }  
  37. }  

code

Step 6:

  • Add a button for shoing the label with command property and bind that command which we have created in view model.

  • Add another button for hiding the label with command property and bind that command which we have created in view model.

  • Add a label in the TestView page and bind its visibility with the model property name with some other properties of binding like Mode, NotifyOnSourceUpdated and UpdateSourceTrigger.

  1. <Button Content="Show Me" Command="{Binding ShowCommand}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="31,60,0,0"/>  
  2.   
  3. <Button Content="Hide Me" Command="{Binding HideCommand}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="148,60,0,0"/>  
  4.   
  5. <Label Visibility="{Binding TestModel.MessageVisibilty,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  
  6.   
  7. Content="I am Here" Margin="89,124,-89,-124"/>  

code

Step 7: Add PrismMVVMTestProject.ViewModels namespace and bind Data Context of TestView Page to the ViewModel named ‘TestViewModel’.

  1. using System.Windows;  
  2. using PrismMVVMTestProject.ViewModels;  
  3. namespace PrismMVVMTestProject.Views  
  4. {  
  5.     /// <summary>  
  6.     /// Interaction logic for TestView.xaml  
  7.     /// </summary>  
  8.     public partial class TestView : Window  
  9.     {  
  10.         public TestView()  
  11.         {  
  12.             InitializeComponent();  
  13.             this.DataContext = new TestViewModel();  
  14.         }  
  15.     }  
  16. }  

code

Step 8: Change the ‘StartupUri’ from default page ‘MainWindow’ to ‘TestView’ page,

TestView

Run the page and see the output:

Output

After click on the ‘Hide Me’ button, it will hide the label,

Hide Me

And when you click on the ‘Hide Me’ button, it will hide the label,

Hide Me

Tip: You can also use ‘Visibility.Collapsed in place of ‘Visibility.Hidden. In the hidden space of control will remain of the hidden control but in collapsed the space of control will not remain.

Read more articles on WPF:

Up Next
    Ebook Download
    View all
    Learn
    View all