I'm still trying to grok, as they say, MVVM in Silverlight. So if any of
this sounds ignorant, don't be shy about letting me know.
So essentially I have a ViewModel such as this:
public class MainLayoutViewModel : ViewModelBase
{
private LoginViewModel m_loginController = new LoginViewModel();
private TestViewModel m_testController = new TestViewModel();
private ViewModelBase m_currentViewModel;
public MainLayoutViewModel()
{
CurrentViewModel = m_loginController;
}
public ViewModelBase CurrentViewModel
{
get { return m_currentViewModel; }
set
{
m_currentViewModel = value;
InvokePropertyChanged("CurrentViewModel");
}
}
}
Now when my LoginViewModell "authenticates", I suppose I want to change the
"CurrentViewModel" property to a new view model. In my View, I would like
to change the DataTemplate "View" to the matching ViewModel, but it seems
something like this is not supported in SL. I found some great support for
this in the SL Extensions <http://slextensions.net> (ResourceSelector), *but
I was wondering how everyone else does this, or if there is a better way* or
if I'm just doin' it wrong? My View with the SL extension looks like this:
<UserControl x:Class="SKMEventUI.MainLayoutView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Views="clr-namespace:MyNamespace.Views"
xmlns:Data="clr-namespace:SLExtensions.Data;assembly=SLExtensions"
xmlns:Controls="clr-namespace:SLExtensions.Controls;assembly=SLExtensions.C ontrols">
<UserControl.Resources>
<Data:ResourceSelector x:Key="controlTemplateSelector">
<ResourceDictionary>
<DataTemplate x:Key="LoginViewModel">
<Border BorderBrush="Black" BorderThickness="1,1,1,1">
<Views:LoginView/>
</Border>
</DataTemplate>
<DataTemplate x:Key="TestViewModel">
<Views:TestView />
</DataTemplate>
</ResourceDictionary>
</Data:ResourceSelector>
<DataTemplate x:Key="controlItemTemplate">
<ContentControl Content="{Binding}"
ContentTemplate="{Binding
Converter={StaticResource controlTemplateSelector}}" />
</DataTemplate>
</UserControl.Resources>
<StackPanel x:Name="LayoutRoot" Background="White">
<ContentControl Content="{Binding CurrentViewModel}"
ContentTemplate="{StaticResource
controlItemTemplate}" />
</StackPanel>
</UserControl>