簡體   English   中英

根據ListBox選擇更改ContentTemplate

[英]Changing ContentTemplate based on ListBox selection

我在StackPanel中有一個Listbox和一個邊框,類似於以下內容:

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

當我在列表框中選擇一個項目時,我想相應地更改ContentPresenter中的內容,例如,選擇人們將更改模板以顯示與人相關的一系列輸入字段,其中選擇動物將顯示與動物等相關的一系列字段。 - 這種行為類似於TabControl。

我想我可以使用DataTrigger實現這一點,它改變了Border中的DataTemplate,但我不知道如何實現這一點。

有什么指針嗎?

謝謝

您可以使用DataTrigger切換ContentTemplate,如下所示。
注意,我正在使用一個名為Name的屬性將ObservableCollection綁定到一個簡單對象(Thing),並使用ViewModel將ContentControl的Content綁定到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")
        });
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM