第一种方式
在MainPage.xaml.cs里:
using System.Collections.Generic;
using Windows.UI.Xaml.Controls;
namespace TestDataTemplate
{
public class Student_DataType //第一步,设置数据属性
{
public int Number { get; set; }
public string Name { get; set; }
public string From { get; set; }
}
public class StudentManager //第二步,添加数据
{
public static List<Student_DataType> GetStudent()
{
var student = new List<Student_DataType>();
student.Add(new Student_DataType { Number = 1, Name = "张三", From = "广州" });
student.Add(new Student_DataType { Number = 2, Name = "李四", From = "长沙" });
student.Add(new Student_DataType { Number = 3, Name = "王五", From = "深圳" });
return student;
}
}
public sealed partial class MainPage : Page
{
private List<Student_DataType> student_ItemsSource; //第三步,绑定数据
public MainPage()
{
this.InitializeComponent();
student_ItemsSource = StudentManager.GetStudent();
}
}
}
在MainPage.xaml里:
<Page
x:Class="TestDataTemplate.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestDataTemplate"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<GridView ItemsSource="{x:Bind student_ItemsSource}" Margin="0,50,0,0">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:Student_DataType">
<Border BorderBrush="DeepSkyBlue" BorderThickness="5" Width="210" >
<StackPanel Orientation="Vertical" Width="200">
<TextBlock FontSize="24" Text="{x:Bind Name}" HorizontalAlignment="Center"/>
<TextBlock FontSize="24" Text="{x:Bind From}" HorizontalAlignment="Center"/>
</StackPanel>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
</Page>
第二种方式
当GridView控件数据模板不在控件里,MainPage.xaml需要修改代码如下:
<Page
x:Class="TestDataTemplate.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestDataTemplate"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<DataTemplate x:DataType="local:Student_DataType" x:Key="StudentDataTemplate">
<Border BorderBrush="DeepSkyBlue" BorderThickness="5" Width="210" >
<StackPanel Orientation="Vertical" Width="200">
<TextBlock FontSize="24" Text="{x:Bind Name}" HorizontalAlignment="Center"/>
<TextBlock FontSize="24" Text="{x:Bind From}" HorizontalAlignment="Center"/>
</StackPanel>
</Border>
</DataTemplate>
</Page.Resources>
<Grid>
<GridView ItemsSource="{x:Bind student_ItemsSource}" Margin="0,50,0,0"
IsItemClickEnabled="True"
ItemTemplate="{StaticResource StudentDataTemplate}"
>
</GridView>
</Grid>
</Page>
第三种方式
当GridView控件数据模板不在MainPage.xaml里
1.在“解决方案”中点击选中项目名称,新建两个文件夹,分别是“DataStyles”和Models。“DataStyles”文件夹用于存放控件模板样式xaml,Models用于存放数据属性的类。
2.在DataStyles文件夹新建“Student_DataType”类(c#类),在Models文件夹新建“DictionaryStudent.xaml”(资源字典)。
如图所示:
在Student_DataType.cs:
namespace TestDataTemplate.Models
{
public class Student_DataType
{
public int Number { get; set; }
public string Name { get; set; }
public string From { get; set; }
}
}
在DictionaryStudent.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestDataTemplate"
xmlns:dataSource="using:TestDataTemplate.Models"
>
<DataTemplate x:DataType="dataSource:Student_DataType" x:Key="StudentDataTemplate">
<Border BorderBrush="DeepSkyBlue" BorderThickness="5" Width="210" >
<StackPanel Orientation="Vertical" Width="200">
<TextBlock FontSize="24" Text="{x:Bind Name}" HorizontalAlignment="Center"/>
<TextBlock FontSize="24" Text="{x:Bind From}" HorizontalAlignment="Center"/>
</StackPanel>
</Border>
</DataTemplate>
</ResourceDictionary>
在MainPage.xaml.cs:
using System.Collections.Generic;
using TestDataTemplate.Models;
using Windows.UI.Xaml.Controls;
namespace TestDataTemplate
{
public sealed partial class MainPage : Page
{
private List<Student_DataType> student_ItemsSource;
public MainPage()
{
this.InitializeComponent();
student_ItemsSource.Add(new Student_DataType { Number = 1, Name = "张三", From = "广州" });
student_ItemsSource.Add(new Student_DataType { Number = 2, Name = "李四", From = "长沙" });
student_ItemsSource.Add(new Student_DataType { Number = 3, Name = "王五", From = "深圳" });
}
}
}
在MainPage.xaml:
<Page
x:Class="TestDataTemplate.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestDataTemplate"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///DataStyles/DictionaryStudent.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
<Grid>
<GridView ItemsSource="{x:Bind student_ItemsSource}" Margin="0,50,0,0"
IsItemClickEnabled="True"
ItemTemplate="{StaticResource StudentDataTemplate}"
>
</GridView>
</Grid>
</Page>
第三种是自己想出来的方式,出现了问题如下:
1.分析:明明引用到了Models文件夹里的命名空间,写代码时dataSource还自动识别出Student_DataType,却提示如下问题。
我猜测的问题是:
x:DataType不能再资源字典文件里引用自定义的数据类型
2.分析:在网上找了问题说是复制粘贴代码或者文件就会导致识别不了InitalizeComponent,但这是我明明亲手敲的代码,而且之前写代码时并没有报错之类的,就过了一段时间,居然就识别不了InitalizeComponent。点击“显示可能的修补程序”,却没有发现可引用的东西。
问题已经找到:
由于资源字典文件x:DataType里无法找到Student_DataType名称,而导致初始化控件时而报错。
第三种方式,我尝试了半个小时都未能解决这个问题。由于刚接触UWP,很多地方以及环境和开发文档难免会有一些差别。若有大佬知道其中原因,请多多指教,谢谢!
效果图(没有用到第三种方式)