5.2.1 WPF 通过ItemControl自己做柱状图

1.

最终效果如下图:

5.2.1 WPF 通过ItemControl自己做柱状图_第1张图片

1.1 准备数据 ViewModel

  public class PrimaryItemModel
  {
      public double Value { get; set; }
      public string XLabel { get; set; }
  }
 public class MainViewModel
 {
     public ObservableCollection PrimaryList { get; set; }

     public MainViewModel()
     {
         Random random = new Random();
         PrimaryList = new ObservableCollection();
         for (int i = 0; i < 15; i++)
         {
             PrimaryList.Add(new PrimaryItemModel
             {
                 XLabel = DateTime.Now.ToString("mm:ss"),
                 Value = random.Next(30, 200)
             });
         }
         Task.Run(async () =>
         {
             while (true)
             {
                 await Task.Delay(2000);
                 Application.Current.Dispatcher.Invoke(() =>
                 {
                     PrimaryList.Add(new PrimaryItemModel
                     {
                         XLabel = DateTime.Now.ToString("mm:ss"),
                         Value = random.Next(30, 200)

                     });
                     PrimaryList.RemoveAt(0);

                 });
             }
         });   
     }
 }

    
        
        
    
    
        
            
                
            
        
        
            
                
                    
                        
                        
                    
                    
                        
                        
                    
                    
                    
                
                
                    
                        
                    
                
            
           
        
        
       

你可能感兴趣的:(C#,wpf,java,开发语言)