2

私の仕事は、DataTemplate リストを作成し、ビューを変更するためのボタンを作成することです。「Data」クラスと「FootballTeam」クラスがあり、静的リソースもあります。ボタン イベントのヘルプが必要です。現在のテンプレートを変更するにはどうすればよいですか?

ヒントとして、例ではこの方法を使用するように指示されています。

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

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 に答える