WPF中基于CommunityToolkit.Mvvm 框架的Datagrid动态变化实现

前言

        MVVM是在WPF开发中常用的一种模式,本文介绍基于微软官方的一款MVVM框架CommunityToolkit.Mvvm实现datagrid控件的动态变换

安装

在NuGet管理器中搜索CommunityToolkit.Mvvm 并安装

WPF中基于CommunityToolkit.Mvvm 框架的Datagrid动态变化实现_第1张图片

 页面

WPF中基于CommunityToolkit.Mvvm 框架的Datagrid动态变化实现_第2张图片

 HTML

 
            
                
                
            

            
                
                    
                    
                    
                        
                            
                                
                                    
                                    
                                
                            
                        
                    
                
            
            
                
                
            
        

MainWIndow后台代码

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext=new MyViewModel();
            //注册
            WeakReferenceMessenger.Default.Register(this, OnReceive);
        }
        
        private void OnReceive(object recipient, MessageModel message)
        {
            
            MessageBox.Show(message.Message+message.Date);
        }
    }

 

新建类

新建ColorModel,MessageModel,MyViewModel三个类

WPF中基于CommunityToolkit.Mvvm 框架的Datagrid动态变化实现_第3张图片

 ColorModel

public class ColorModel
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }

messageModel 

internal class MessageModel
    {
            public string Title { get; set; }
            public string Message { get; set; }
            public int Id { get; set; }
            public DateTime Date { get; set; }
      
    }

MyViewModel 

public class MyViewModel: ObservableObject
    {
        public MyViewModel()
        {
            Name = "holle";
            ShowMyCommand = new RelayCommand(Show);
            timer.Elapsed += timer_Tick;
            timer.Interval = 1000;

            Datalist.Add(new ColorModel() { Code = "#9F5F9F", Name = "#蓝紫色" });
            Datalist.Add(new ColorModel() { Code = "#B5A642", Name = "#黄铜色" });
            Datalist.Add(new ColorModel() { Code = "#D9D919", Name = "#亮金色" });
            Datalist.Add(new ColorModel() { Code = "#5F9F9F", Name = "#士官服蓝色" });
            Datalist.Add(new ColorModel() { Code = "#D98719", Name = "#冷铜色" });

           
        }
        public RelayCommand ShowMyCommand { get; set; }//命令

        private System.Timers.Timer timer { get; set; } = new System.Timers.Timer();
        private string name;
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged();//属性通知
            }
        }
        private ObservableCollection datalist=new ObservableCollection();
        public ObservableCollection Datalist
        {
            get { return datalist; }
            set
            {
                datalist = value;
                OnPropertyChanged();//属性通知
            }
        }
        public void Show()
        {
           
            if(!timer.Enabled)
            {
                timer.Stop();
                Name = "定期时器关闭";
            }
            {
                timer.Start();
                Name = "定期时器启动";
            }
            //发送Messge
            WeakReferenceMessenger.Default.Send(new MessageModel() { Date=DateTime.Now,Message= Name });
        }
        private void timer_Tick(object sender,EventArgs e)
        {
            System.Windows.Application.Current.Dispatcher.Invoke((Action)(() =>
            {
                Datalist.Insert(0,Datalist[Datalist.Count - 1]);
                Datalist.RemoveAt(Datalist.Count-1);
            }));

           
          
        }
       
    }

你可能感兴趣的:(C#学习,wpf)