I have a simple WPF application as below:
I also created 3 different views:
- DetailType1.xaml
- DetailType2.xaml
- DetailType3.xaml
and each view has it's own ViewModel
ParentView.xaml
...
<!-- Detail Area -->
<GroupBox x:Name="groupDetails" Grid.Column="0" Header="Details"
HorizontalAlignment="Stretch"
Grid.Row="0" VerticalAlignment="Stretch">
<GroupBox.Resources>
<ResourceDictionary>
<DataTemplate DataType="{x:Type vm:DetailType1ViewModel}">
<views:DetailType1View/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:DetailType2ViewModel}">
<views:DetailType2View/>
</DataTemplate>
</ResourceDictionary>
</GroupBox.Resources>
<ContentPresenter DataContext="{Binding}" Content="{Binding Path=BaseTypeViewModel}" />
</GroupBox>
...
ParentViewModel.cs
...
public BaseViewModel BaseTypeViewModel
{
get { return GetValue<BaseViewModel>(); }
set
{
SetValue(value);
}
}
private void ShowDetailDialog()
{
var vm = GetViewModelByID(SelectedID);
BaseTypeViewModel = vm;
}
private BaseViewModel GetViewModelByID(int Id)
{
switch (Id)
{
case 1:
return IoC.Get<DetailType1ViewModel>();
case 2:
return IoC.Get<DetailType2ViewModel>();
}
}
...
DetailType1ViewModel.cs
public class DetailType1ViewModel : BaseViewModel
{
...
}
My question is:
Everytime I double-click row of DataGrid on the left pane, I want to load one of above views into Details area depend on the selected ID. So what are the techniques can be used? It would be nice if you can show me a code sample.
Thanks to all for the help.