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;
                    }
            ));
        }

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

 

posted @ 2014-12-01 21:14  Blackeye  阅读(462)  评论(0编辑  收藏  举报