zoukankan      html  css  js  c++  java
  • 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;
                        }
                ));
            }

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

  • 相关阅读:
    Linux命令之du命令
    6562. 【GDOI2020模拟4.15】楼房搭建(building)
    CF660E. Different Subsets For All Tuples
    6555. 【GDOI2020模拟4.11】黑红兔(brr)(SAM小技♂巧)
    6554. 【GDOI2020模拟4.11】赢家(winner)
    6553. 【GDOI2020模拟4.11】人生(life)
    6546. 【GDOI2020模拟4.8】USACO 2020 Open Contest, Platinum(circus)
    6545. 【GDOI2020模拟4.8】USACO 2020 Open Contest, Platinum(exercise)
    6544. 【USACO 2020 US Open Platinum】 Sprinklers 2: Return of the Alfalfa
    6543. 【GDOI2020模拟4.4】Easy Data Structure(动态dp)
  • 原文地址:https://www.cnblogs.com/Blackeye286/p/3256067.html
Copyright © 2011-2022 走看看