crazy9
crazy9

Reputation: 377

Trigger a DataTemplate from a Button Click

I have a a Group of Buttons that are dynamically generated from a collection on the ViewModel. And based on what button the user clicks, I should open a different View. What are the different possible approaches I could use. One choice I am thinking is using a DataTemplateSelector. But how can I handle a button click and trigger a Template based on which Button is clicked.

    <DataTemplate x:Key="viewOneTemplate">
        <details:ViewOne x:Name="viewOne"/>
    </DataTemplate>

       <DataTemplate x:Key="viewTwoTemplate">
        <details:ViewTwo x:Name="viewTwo"/>
    </DataTemplate>

           <DataTemplate x:Key="transitionContentTemplate">
                <ItemsControl ItemsSource="{Binding Path=TransitionItems}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <WrapPanel>
                                <Button Content="{Binding DisplayName}"/>
                            </WrapPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>

Upvotes: 0

Views: 497

Answers (1)

Abin
Abin

Reputation: 2956

I Think you have to use Content Presenter to change the DataTemplate on Button Command

 <Window>
 <Window.Resources>
   <DataTemplate DataType="{x:Type local:VM1}">
      <!-- View 1 Here -->
   </DataTemplate>
   <DataTemplate DataType="{x:Type local:VM2}">
      <!-- View 2 here -->
   </DataTemplate>
<Window.Resources>
<ContentPresenter Content="{Binding CurrentVM}"/>
</Window>

Now from your Button Command change the CurrentVM property, Make sure your CurrentVM property type should be of object.

Referance

Upvotes: 1

Related Questions