ListView数据动态更新

摘要:
我们经常使用与数据和前台控件绑定相关的问题。我们总是知道我们需要使用委托(代理),但我们每次都会忘记它。每次我们一次又一次地使用百度时,我们的记忆都不长。这一次,我们必须认真地写下来,下次我们不会忘记它。后台数据和前台数据的绑定主要分为两个步骤:第一步是将要绑定的数据定义为数据集或对象,并将其绑定到List以便于调用:publicclassTestCase:INotifyPropertyChanged{publiceventPro

      经常会用到数据与前台控件绑定相关的问题,一直知道要用委托(代理)但每次都忘,而且每次都百度了一遍又一遍,就是不长记性,这次一定好好记下来下次就不会忘了。

      后台数据与前台绑定主要分为两步:

      第一步把要绑定的数据定义为一个数据集合或对象绑定到List中,方便调用:

public class TestCase:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string caseID;
        private string commFile;
        private string commParameter;
        private string commFlag;
        private string startTime;
        private string endTime;
        private string executionTime;
        private string executionResult;

        public string CaseID
        {
            get { return caseID; }
            set 
            { 
                caseID = value;
                if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CaseID"));
            }
        }

        public string CommFile
        {
            get { return commFile; }
            set 
            {
                commFile = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CommFile"));
            }
        }

        public string CommParameter
        {
            get { return commParameter; }
            set 
            {
                commParameter = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CaseID"));
            }
        }

        public string CommFlag
        {
            get { return commFlag; }
            set
            {
                commFlag = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CommFlag"));
            }
        }

        public string StartTime
        {
            get { return startTime; }
            set 
            { 
                startTime = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("StartTime"));
            }
        }

        public string EndTime
        {
            get { return endTime; }
            set 
            { 
                endTime = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("EndTime"));
            }
        }

        public string ExecutionTime
        {
            get { return executionTime; }
            set
            { 
                executionTime = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ExecutionTime"));
            }
        }

        public string ExecutionResult
        {
            get { return executionResult; }
            set 
            { 
                executionResult = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ExecutionResult"));
            }
        }
    }

 对象创建好了就可以第二步实现数据一条一条更新了:

       /// <summary>
        /// Set Listview Value
        /// </summary>
        /// <param name="lv"></param>
        /// <param name="obj"></param>
        public static void UpdateListViewValue(ListView lv, Object obj)
        {
            lv.Dispatcher.Invoke(
                new Action(
                    delegate
                    {
                        lv.Items.Add(obj);
                        lv.ScrollIntoView(obj);
                    }
                    ));
        }
        /// <summary>
        /// Set ProgressBar Value
        /// </summary>
        /// <param name="pb"></param>
        /// <param name="value"></param>
        public static void UpdateProgressBarValue(ProgressBar pb, double value)
        {
            pb.Dispatcher.Invoke(
               new Action(
                 delegate
                 {
                     pb.Visibility = Visibility.Visible;
                 }
            ));
            pb.Dispatcher.Invoke(new Action<System.Windows.DependencyProperty, object>
            (pb.SetValue), System.Windows.Threading.DispatcherPriority.Background, System.Windows.Controls.ProgressBar.ValueProperty,
                value
            );
        }
        /// <summary>
        /// Set Label Value
        /// </summary>
        /// <param name="lb"></param>
        /// <param name="value"></param>
        public static void UpdateLableValue(Label lb, string value)
        {
            lb.Dispatcher.Invoke(
                new Action(
                    delegate
                    {
                        lb.Visibility = Visibility.Visible;
                        lb.Content = value;
                    }
            ));
        }

这下好了,记下来就不怕忘了。

免责声明:文章转载自《ListView数据动态更新》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【腾讯Bugly干货分享】打造“微信小程序”组件化开发框架Java getResourceAsStream返回为空的问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

RSA加密解密及RSA签名和验证

此Demo包含两个文件,建立一个解决方案,然后建立两个文件,一个为Form,一个为Class,把代码分别复制进去即可 RSA正确的执行过程:加密解密:1、获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥2、加密3、解密签名和验证:签名:1、获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥2、获取待签名的Hash码3、签名其中...

C#使用Process类杀死进程,执行命令等

c#之process类相关整理 一、根据进程名获取进程的用户名? 需要添加对 System.Management.dll 的引用 using System.Diagnostics; using System.Management; static void Main(string[] args) { foreach (Process p in Proc...

fastreport对象的属性和方法

对象的属性和方法 报表的报表可视对象都是TfrView的继承类。在脚本中可以使用下列属性和方法: 属性 类型 描    述 BandAlign Integer 设置对象在band中的位置。 可选值:baNone, baLeft, baRight, baCenter, baWidth, baBottom。 Enabled Boolean...

word的常用操作

using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word;using System.IO;using System.Web;using System.Data;using System.Reflection;usin...

Java 中使用Jackson反序列化

Build.gradle: compile group: 'org.codehaus.jackson', name: 'jackson-mapper-lgpl', version: '1.9.13' compile group: 'org.codehaus.jackson', name: 'jackson-core-lgpl', version: '...

mysql-5.6.27源码安装及错误解决办法

环境:centos6.5.x86_64 wgethttp://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.27.tar.gz yum install -y cmake 当然也可以自己下载源码包安装,为方便就Yum安装了 useradd -s /sbin/nologin mysql tar zxvf mysql-...