0

Possible Duplicate:
Changing the View for a ViewModel

I have a view:

<UserControl ...>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ComboBox ItemSource="{Binding Items}" />
        **<???>**
    </Grid>
</UserControl>

I have ViewModel:

public class VM
{
    // ...
    public List<Entities> Items { get; set;}
    public String Title { get; set; }
}

and I have a few subview's like this:

<UserControl ...>
    <TextBlock Text="{Binding Title}" />
</UserControl>

When user selecta some value from ComboBox in main View, I need to place in second column of main View some of subViews. If user selects other value in ComboBox, another subView sould replace existing subView.

How can it be done?

Community
  • 1
  • 1
Lari13
  • 1,850
  • 10
  • 28
  • 55
  • 1
    If your question is similar to another which I've already answered: http://stackoverflow.com/questions/5309099/changing-the-view-for-a-viewmodel/5310213#5310213. I can rewrite it so it'll use the combobox for view switching. – vortexwolf Oct 03 '11 at 09:48

2 Answers2

1

I usually just use a ContentControl and let it figure out which view to draw based on DataTemplates

<ContentControl Content="{Binding SelectedView}">
    <ContentControl.Resources>
        <DataTemplate DataType="{x:Type local:ViewModelA}">
            <local:ViewA />
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:ViewModelB}">
            <local:ViewB />
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:ViewModelC}">
            <local:ViewC />
        </DataTemplate>
    </ContentControl.Resources>
</ContentControl>
Rachel
  • 130,264
  • 66
  • 304
  • 490
0

You could bind to the SelectedItem of the ComboBox, e.g.

<ComboBox x:Name="cb" ItemSource="{Binding Items}" />
<ContentControl Content="{Binding SelectedItem, ElementName=cb}" Grid.Column="1">
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <v:SubView /> <!-- Bind properties as appropriate -->
        <DataTemplate>
    <ContentControl.ContentTemplate>
<ContentControl>

If you have differnt views use the ContentTemplateSelector instead of hardcoding one template.

H.B.
  • 166,899
  • 29
  • 327
  • 400