首页 > 代码库 > ListView数据动态更新
ListView数据动态更新
经常会用到数据与前台控件绑定相关的问题,一直知道要用委托(代理)但每次都忘,而且每次都百度了一遍又一遍,就是不长记性,这次一定好好记下来下次就不会忘了。
后台数据与前台绑定主要分为两步:
第一步把要绑定的数据定义为一个数据集合或对象绑定到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数据动态更新
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。