1

Hi have 2 usercontrol(WPF). I have to load this control according to condition. I have ReadingBookDoubleView.xaml nad ReadingBookDoubleViewpdf.xaml this is my code.

   <UserControl.Resources>
    <DataTemplate DataType="{x:Type viewModels:ReadingBookDoubleVM}">
        <view:ReadingBookDoubleViewPdf/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type viewModels:ReadingBookDoubleVM}">
        <view:ReadingBookDoubleView/>
    </DataTemplate>
</UserControl.Resources>

I have book kind in Viewmodel class which is bind to this view where I am loading the user control. i have to load one control at a time.If book Kind is Pdf then I have load ReadingBookDoubleViewpdf control other wise I have to load ReadingBookDoubleView.

how can I load the control according to condition.

Jitendra singh
  • 413
  • 11
  • 27
  • possible duplicate of [Conditional DataTemplate](http://stackoverflow.com/questions/9049197/conditional-datatemplate) – Sinatr Jun 23 '15 at 12:30
  • Have the 2 ViewModel inherite from an inteface or abstract class then bind the abtract class or interface, when binding the concrete call it will use the matching view. Edit: Ignore this comment Didnt notice it was the same viewmodel – Henrik Bøgelund Lavstsen Jun 23 '15 at 12:31
  • It doesn't make sense the way you're trying to do it. You're basically saying: If you find a `ReadingBookDoubleVM`, display A, and if you find the same (`ReadingBookDoubleVM`), display B. no sense. – Noctis Jun 23 '15 at 12:45
  • You cannot do so since you have same viewmodel associated with in both the datatemplates. Refer my post here http://social.technet.microsoft.com/wiki/contents/articles/30898.simple-navigation-technique-in-wpf-using-mvvm.aspx – Ayyappan Subramanian Jun 23 '15 at 13:10

1 Answers1

0

You could use a single DataTemplate with a Trigger:

<UserControl.Resources>
    <DataTemplate DataType="{x:Type viewModels:ReadingBookDoubleVM}">
        <ContentControl x:Name="Presenter" Content="{Binding}">
            <ContentControl.ContentTemplate>
                <DataTemplate>
                    <view:ReadingBookDoubleView />
                </DataTemplate>
            </ContentControl.ContentTemplate>
        </ContentControl>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Kind}" Value="Pdf">
                <Setter TargetName="Presenter"
                        Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <view:ReadingBookDoubleViewPdf />
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</UserControl.Resources>
almulo
  • 4,918
  • 1
  • 20
  • 29