2

my task is to make DataTemplate list, and create a button for changing the view. I have the "Data" and "FootballTeam" classes, and also I have the Static Resources. I need help for the button event, how can I change the current template?

As a tip, the example says to use this method:

"this.Resources[resource-key] as data-type;"

The XAML:

<Window x:Class="WpfApplication11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="250"
        Width="300">
  <Window.Resources>
    <DataTemplate x:Key="teamName">
      <TextBlock FontWeight="Bold"
                 Text="{Binding Path=TeamName}"></TextBlock>
    </DataTemplate>
    <DataTemplate x:Key="year">
      <TextBlock Text="{Binding Path=FoundingYear}"></TextBlock>
    </DataTemplate>
    <DataTemplate x:Key="logo">
      <Image Source="{Binding Path=Image}" />
    </DataTemplate>

  </Window.Resources>
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <ScrollViewer  Grid.Row="0"
                   AllowDrop="True">
      <ListBox Name="lstTeams">

      </ListBox>
    </ScrollViewer>
    <Button Grid.Row="1"
            Margin="6">Change View</Button>
  </Grid>
</Window>
4

1 回答 1

2

我猜你想改变列表框模板,试试这个:

在 XAML 中

<Button Grid.Row="1" Margin="6" Click="changeTemplate">Change View</Button>

在 C# 中

lstTeams.ItemTemplate = (DataTemplate)this.Resources["teamname"];

您必须处理要循环浏览的不同模板,但这几乎是如何在代码隐藏中进行的。

于 2013-04-18T09:25:33.673 回答