根据 ListBox 选择更改 ContentTemplate

4

我有一个类似以下结构的StackPanel,其中包含一个Listbox和一个Border:

<StackPanel Orientation="Horizontal">
      <ListBox>
          <ListBoxItem Content="People"/>
          <ListBoxItem Content="Animals"/>
          <ListBoxItem Content="Cars"/>
       </ListBox>
       <Border Width="200>
            <ContentPresenter/>
       </Border>
</StackPanel>

当在列表框中选择项目时,我希望能相应地更改ContentPresenter中的内容,例如选择“People”将更改模板以显示与人有关的一系列输入字段,而选择“Animals”将显示与动物相关的一系列字段等等。-这种行为类似于TabControl。
我认为可以通过DataTrigger实现此目的,该DataTrigger会更改Border中的DataTemplate,但我不确定如何实现此目标。
有任何建议吗?
谢谢。
1个回答

9
您可以使用DataTrigger来切换ContentTemplate,如下所示。
请注意,我将ObservableCollection绑定到一个简单对象(Thing),该对象具有一个名为Name的属性,并使用ViewModel将ContentControl的内容绑定到ListBox中的SelectedItem。
<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>

这里是Thing类:
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),然后再更改模板,但它立即更改了。 - Sidebp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接