前言
MVVM是在WPF开发中常用的一种模式,本文介绍基于微软官方的一款MVVM框架CommunityToolkit.Mvvm实现datagrid控件的动态变换
安装
在NuGet管理器中搜索CommunityToolkit.Mvvm 并安装
页面
HTML
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<DataGrid x:Name="grid" ItemsSource="{Binding Datalist, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Code" Binding="{Binding Code}"></DataGridTextColumn>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"></DataGridTextColumn>
<DataGridTemplateColumn Header="操作">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Width="10" Height="10" Background="{Binding Code}"></Border>
<TextBlock Margin="10,0" Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
<TextBox Height="100" Width="200" Background="Yellow" Text="{Binding Path=Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<Button Width="200" Content="开始" Height="100" Command="{Binding ShowMyCommand}"></Button>
</StackPanel>
</Grid>
MainWIndow后台代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext=new MyViewModel();
//注册
WeakReferenceMessenger.Default.Register<MessageModel>(this, OnReceive);
}
private void OnReceive(object recipient, MessageModel message)
{
MessageBox.Show(message.Message+message.Date);
}
}
新建类
新建ColorModel,MessageModel,MyViewModel三个类
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<ColorModel> datalist=new ObservableCollection<ColorModel>();
public ObservableCollection<ColorModel> 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);
}));
}
}