【问题标题】:wpf - Data Template, change template with buttonwpf - 数据模板,使用按钮更改模板
【发布时间】:2013-04-11 06:35:11
【问题描述】:

我的任务是制作 DataTemplate 列表,并创建一个用于更改视图的按钮。 我有“数据”和“足球队”类,还有静态资源。我需要按钮事件的帮助,如何更改当前模板?

作为提示,该示例说要使用此方法:

"this.Resources[resource-key] 作为数据类型;"

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>

【问题讨论】:

标签: c# wpf xaml


【解决方案1】:

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

在 XAML 中

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

在 C# 中

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

您必须处理要循环浏览的不同模板,但这几乎是代码隐藏的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2018-11-01
    • 2011-05-08
    • 2011-03-14
    • 2019-04-05
    相关资源
    最近更新 更多