【发布时间】: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>
【问题讨论】:
-
看看我对类似问题的回答。它演示了如何使用按钮和数据模板更改视图stackoverflow.com/a/15960843/1862333