Different views / data template based on member variable

后端 未结 2 1615
轻奢々
轻奢々 2020-11-30 03:44

I have a view model called

 ViewModelClass 

which contains a boolean.

I have another view model which contains

Observab         


        
相关标签:
2条回答
  • 2020-11-30 03:57

    if you want to switch your itemscontrol item view in case of the ViewModelClass boolean, then you can simply use a datatrigger style in your ProjectInfoView usercontrol.

    0 讨论(0)
  • 2020-11-30 03:59

    I usually use a ContentControl to display the data, and swap out the ContentTemplate in a trigger based on the property that changes.

    Here's an example I have posted on my blog that swaps a template based on a bound property

    <DataTemplate x:Key="PersonTemplate" DataType="{x:Type local:ConsumerViewModel}">
         <TextBlock Text="I'm a Person" />
    </DataTemplate> 
    
    <DataTemplate x:Key="BusinessTemplate" DataType="{x:Type local:ConsumerViewModel}">
         <TextBlock Text="I'm a Business" />
     </DataTemplate>
    
    <DataTemplate DataType="{x:Type local:ConsumerViewModel}">
         <ContentControl Content="{Binding }">
             <ContentControl.Style>
                 <Style TargetType="{x:Type ContentControl}">
                     <Setter Property="ContentTemplate" Value="{StaticResource PersonTemplate}" />
                     <Style.Triggers>
                         <DataTrigger Binding="{Binding ConsumerType}" Value="Business">
                             <Setter Property="ContentTemplate" Value="{StaticResource BusinessTemplate}" />
                         </DataTrigger>
                     </Style.Triggers>
                 </Style>
             </ContentControl.Style>
         </ContentControl>
     </DataTemplate>
    

    A DataTemplateSelector will also work, but only if the property that determines which template to show doesn't change since DataTemplateSelectors don't respond to change notifications. I usually avoid them if possible since I also prefer my view selection logic in my view so I can see whats going on.

    0 讨论(0)
提交回复
热议问题