How to filter the views in a region based on condition?

Topics: Prism v1, Prism v2 - Silverlight 2, Prism v2 - Silverlight 3, Prism v2 - Silverlight 4, Prism v2 - WPF 3.5, Prism v2 - WPF 4, Prism v4 - Silverlight 4, Prism v4 - WPF 4
Jul 13, 2011 at 5:31 PM

Hi

There is a regions container control(Scoped region). For example, class A. A has a property and a region. There are many views will be imported into the region. My purpose is the region only imports some of the defined views based on the condition(A.Property). Is it possible?

For example, there are 3 views: view1, view2, view3. If the property = 1, import view 1 and view2, if the property = 2, import the view2 and view3.

Any suggestion?

Developer
Jul 14, 2011 at 7:57 PM

Hi,

Out of the box, Prism does not support conditional adding of views to regions. However, you could create a custom service that internally uses the RegionManager to locate your region and conditionally call the Region.Add method to add the views if the conditions specified are met.

If you consider this should be added to Prism as a default capability of the Prism Library, you could create a work item in the Issue Tracker encouraging the product team to add it on a future version.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jul 17, 2011 at 3:24 PM
Edited Jul 17, 2011 at 3:26 PM

Hi jzhongsun,

Try doing like below

in the Resource file

 

 

<vm:MyViewModel x:Key="ViewModel"/>

 

<DataTemplate x:Key="Template1">

   <v:View1></v:View1>

</DataTemplate>

<DataTemplate x:Key="Template2">

   <v:View2></v:View2>

</DataTemplate>

<DataTemplate x:Key="Template3">

   <v:View3></v:View3>

</DataTemplate>


<DataTemplate DataType="{x:Type vm:MyViewModel}">

  <ContentPresenter x:Name="presenter"></ContentPresenter>

  <DataTemplate.Triggers>

     <DataTrigger Binding="{Binding MyProperty}" Value="1">

         <Setter TargetName="presenter" Property="ContentTemplate" Value="{StaticResource Template1}"></Setter>

     </DataTrigger>

    <DataTrigger Binding="{Binding MyProperty}" Value="2">

         <Setter TargetName="presenter" Property="ContentTemplate" Value="{StaticResource Template2}"></Setter>

     </DataTrigger>

    <DataTrigger Binding="{Binding MyProperty}" Value="3">

         <Setter TargetName="presenter" Property="ContentTemplate" Value="{StaticResource Template3}"></Setter>

     </DataTrigger>

   </DataTemplate.Triggers>

</DataTemplate>

 

 

In the MainWindow.xaml do below

 

 

<ContentControl Content="{StaticResource ViewModel}" />

 

 

In your viewmodel declare MyProperty property and change the value dynamically and it will switch your view.

 

Thanks

Ashish