UWP ListView数据绑定
前往我的主页以获得更好的阅读体验,并获取项目源文件UWP ListView数据绑定 - DearXuan的主页
https://blog.dearxuan.com/2021/10/11/UWP-ListView%E6%95%B0%E6%8D%AE%E7%BB%91%E5%AE%9A/
在制作UWP个人项目时需要用到数据绑定,网上的教程大都不全,特此记录下自己使用的方法。
绑定源
指定一个类用来保存数据,以我自己的项目为例,需要定义“邮件”类。
MailSystem_UWP.Bean.Email
namespace MailSystem_UWP.Bean
{/** mail_table:* id:int* sender:varchar(10)* receiver:varchar(10)* title:varchar(50)* content:varchar(500)* time:datetime* state:int*/public class Email{public enum STATE{Delivery = 0,//送达RecipientNotFound = 1,//找不到收件人Deleted = 2//已被删除}public int id { get; set; }public string sender { get; set; }public string receiver { get; set; }public string title { get; set; }public string content { get; set; }public DateTime time { get; set; }public STATE state { get; set; }public Email() { }public Email(MySqlDataReader reader){LoadFromReader(reader);}/// /// 从MySqlDataReader获取User实例/// /// MySqlDataReader实例public void LoadFromReader(MySqlDataReader reader){id = reader[0].ToString().ToInt();sender = reader[1].ToString();receiver = reader[2].ToString();title = reader[3].ToString();content = reader[4].ToString();time = reader.GetDateTime(5);state = (STATE)reader[6].ToString().ToInt();}/// /// 获取概要,用于显示在TextBlock中/// /// string类型:标题+"\n"+概要 public string GetDescription(){return (content.Length <= Value.UserForm.DESCRIPTION_MAX_LENGTH? content: content.Substring(0, Value.UserForm.DESCRIPTION_MAX_LENGTH) + "...")+ "\n";}}
}
为绑定源定义集合
private static ObservableCollection data = new ObservableCollection();
当UWP进行了页面跳转,即使使用GoBack()来返回,原页面也会重新加载,因此建议使用静态类来避免数据丢失,并且修改集合时也不需要获取MainPage的实例
创建绑定
在xaml文件头定义命名空间
由于我的Email类在MailSystem_UWP.Bean下,因此定义该命名空间为local
xmlns:local="using:MailSystem_UWP.Bean"
在xaml文件的Page标签内定义模板
{x:Bind name}指向绑定源的name属性,你需要实现name的get和set方法,像这样
public int id { get; set; }
对于非字符串变量,例如System.DateTime,系统会自动调用toString()
DataTemplate内即为数据模板,ListView会根据模板逐一添加控件
现在为ListView加上ItemTemplate属性
在MainPage的构造函数里绑定数据源
public MainPage()
{this.InitializeComponent();listView.ItemsSource = data;
}
添加数据
直接为data添加数据,ListView中会自动更新
data.Clear();
for(int i = 0; i < emails.Count; i++)
{//emails是ArrayList的实例data.Add(emails[i]);
}

完整代码
MailSystem.Bean.Email
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using MailSystem_UWP.Support;namespace MailSystem_UWP.Bean
{/** mail_table:* id:int* sender:varchar(10)* receiver:varchar(10)* title:varchar(50)* content:varchar(500)* time:datetime* state:int*/public class Email{public enum STATE{Delivery = 0,//送达RecipientNotFound = 1,//找不到收件人Deleted = 2//已被删除}public int id { get; set; }public string sender { get; set; }public string receiver { get; set; }public string title { get; set; }public string content { get; set; }public DateTime time { get; set; }public STATE state { get; set; }public Email() { }public Email(MySqlDataReader reader){LoadFromReader(reader);}/// /// 从MySqlDataReader获取User实例/// /// MySqlDataReader实例public void LoadFromReader(MySqlDataReader reader){id = reader[0].ToString().ToInt();sender = reader[1].ToString();receiver = reader[2].ToString();title = reader[3].ToString();content = reader[4].ToString();time = reader.GetDateTime(5);state = (STATE)reader[6].ToString().ToInt();}/// /// 获取概要,用于显示在TextBlock中/// /// string类型:标题+"\n"+概要 public string GetDescription(){return (content.Length <= Value.UserForm.DESCRIPTION_MAX_LENGTH? content: content.Substring(0, Value.UserForm.DESCRIPTION_MAX_LENGTH) + "...")+ "\n";}}
}
MainPage.xaml
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using MailSystem_UWP.View;
using MailSystem_UWP.Bean;
using Windows.UI.Xaml.Media.Imaging;
using System.Windows.Input;
using System.Collections.ObjectModel;// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板namespace MailSystem_UWP
{public sealed partial class MainPage : Page{//已经删除了不相关的代码//数据绑定private static ObservableCollection data = new ObservableCollection();public MainPage(){this.InitializeComponent();listView.ItemsSource = data;}public void LoadEmail(List emails){data.Clear();for(int i = 0; i < emails.Count; i++){data.Add(emails[i]);}}}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
