Changing ContentTemplate based on ListBox selection

前端 未结 1 2083
無奈伤痛
無奈伤痛 2021-02-10 21:17

I have a Listbox and a Border in a StackPanel similar to the following:


      
          

        
相关标签:
1条回答
  • 2021-02-10 21:55

    You can toggle the ContentTemplate using a DataTrigger as follows.
    Note, that I am binding an ObservableCollection to a simple object (Thing) with one property called Name, and am I binding the Content of the ContentControl to the SelectedItem in the ListBox using a ViewModel.

    <Grid>
        <Grid.Resources>
            <local:MultiValueConverter x:Key="con" />
    
            <DataTemplate x:Key="PeopleTemplate">
                <StackPanel Orientation="Horizontal">
                    <Label Margin="0,0,5,0" Content="People Name" HorizontalAlignment="Left" Grid.Column="0" />
                    <TextBox Grid.Column="1" Width="100" Height="25"></TextBox>
                    <Button Content="OK" Grid.Column="2" />
                </StackPanel>
            </DataTemplate>
    
            <DataTemplate x:Key="AnimalsTemplate">
                <StackPanel Orientation="Horizontal">
                    <Label Margin="0,0,5,0" Content="Animal Name" HorizontalAlignment="Left" Grid.Column="0" />
                    <TextBox Grid.Column="1" Width="100" Height="25"></TextBox>
                    <Button Content="OK" Grid.Column="2" />
                </StackPanel>
            </DataTemplate>
    
            <DataTemplate x:Key="CarsTemplate">
                <StackPanel Orientation="Horizontal">
                    <Label Margin="0,0,5,0" Content="Car Name" HorizontalAlignment="Left" Grid.Column="0" />
                    <TextBox Grid.Column="1" Width="100" Height="25"></TextBox>
                    <Button Content="OK" Grid.Column="2" />
                </StackPanel>
            </DataTemplate>
        </Grid.Resources>
    
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
    
        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <ListBox ItemsSource="{Binding Things}" SelectedItem="{Binding SelectedThing}">
                <ListBox.ItemTemplate>
                  <DataTemplate>
                    <StackPanel Margin="0" Orientation="Horizontal">
                        <TextBlock Padding="5" Text="{Binding Name}" Margin="0"></TextBlock>
                    </StackPanel>
                  </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Border Width="200">
                <ContentControl Content="{Binding SelectedThing}">
                        <ContentControl.ContentTemplate>
                        <DataTemplate>
                            <ContentControl Name="cc" 
                              Content="{Binding}" 
                              ContentTemplate="{StaticResource PeopleTemplate}" />
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding Path=Name}" Value="People">
                                    <Setter TargetName="cc"  
                                        Property="ContentTemplate" 
                                        Value="{StaticResource PeopleTemplate}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=Name}" Value="Animals">
                                    <Setter TargetName="cc"  
                                        Property="ContentTemplate" 
                                        Value="{StaticResource AnimalsTemplate}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=Name}" Value="Cars">
                                    <Setter TargetName="cc"  
                                        Property="ContentTemplate" 
                                        Value="{StaticResource CarsTemplate}" />
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </ContentControl.ContentTemplate>
                </ContentControl>
            </Border> 
        </StackPanel>        
    <Grid>
    

    Here is the Thing class:

    public class Thing
    {
      public Thing(String name)
      {
         this.Name = name;
      }
    
      public String Name { get; set; }
    
      public static ObservableCollection<Thing> GetThingList()
      {
         return new ObservableCollection<Thing>(new Thing[3] {
                new Thing("People"), 
                new Thing("Animals"),
                new Thing("Cars")
            });
      }
    }
    
    0 讨论(0)
提交回复
热议问题