Click here to Skip to main content
15,879,184 members
Articles / Desktop Programming / WPF
Tip/Trick

Using an IValueConverter in a WPF MVVM application to select a UserControl/View

Rate me:
Please Sign up or sign in to vote.
4.57/5 (7 votes)
5 Sep 2012CPOL1 min read 29.6K   469   10   7
The IValueConverter can be used to select a View.

I answered a question a week or so ago, and was unable to point to an example of using an IValueConverter to select a View. I said I would publish something, so here it is.

One of the things a value converter can be used for is set the View. A good choice for the property to bind to is the ViewModel. The value converter can then set the View depending on the type of the ViewModel:

class ViewModelViewValueConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, 
    System.Globalization.CultureInfo culture)
  {
    if (value is UserControlViewModel1)
      return new View1();
    else if (value is UserControlViewModel2)
      return new View2();
    return null;
  }

  public object ConvertBack(object value, Type targetType, object parameter, 
    System.Globalization.CultureInfo culture)
  {
    throw new NotImplementedException();
  }
}

The application I have created to show this concept is really basic. Many of the class are empty, including the UserControl ViewModel classes, and the each of the UserControls only contain some text and no extra code behind. The ViewModel for the main form contains only two DelegateCommand properties to change the ViewModel in the ViewModel property to one of the two classes. If you are familiar with binding in WPF, you will see that the MainViewModel inherits from INotifyPropertyChanged since I need to be able to inform the View when the ViewModel property is changed. The only other significant part of the solution is the binding. I used a ContentControl in the ViewModel and bind to its Content property:

<ContentControl Content="{Binding ViewModel, 
                   Converter={StaticResource ValueConverter}}"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch" />

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at qck1@hotmail.com.

Comments and Discussions

 
QuestionIvalue Converter Pin
rezanasiriJahroodi23-Apr-19 0:54
rezanasiriJahroodi23-Apr-19 0:54 
GeneralMy vote of 4 Pin
ZieluONE Marcin Zielinski14-Nov-12 11:55
ZieluONE Marcin Zielinski14-Nov-12 11:55 
GeneralMy vote of 3 Pin
Aslesh5-Sep-12 9:14
Aslesh5-Sep-12 9:14 
not great
AnswerRe: My vote of 3 Pin
Clifford Nelson5-Sep-12 9:49
Clifford Nelson5-Sep-12 9:49 
GeneralRe: My vote of 3 Pin
Tencor Developer12-Jun-18 8:11
Tencor Developer12-Jun-18 8:11 
SuggestionWhy cant u use template selectors? Pin
Aslesh5-Sep-12 9:14
Aslesh5-Sep-12 9:14 
AnswerRe: Why cant u use template selectors? Pin
Clifford Nelson5-Sep-12 9:50
Clifford Nelson5-Sep-12 9:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.