DataGrid绑定数据

本文介绍了如何在MVVM模式下使用WPF的DataGrid进行数据查询和绑定。首先,创建一个继承自查询表的实体类CommodityDetailVo,并实现INotifyPropertyChanged接口以实现实时更新。然后,通过LinQ查询获取数据并赋值给绑定属性。在XAML中,设置DataGrid的ItemsSource为绑定属性,并通过DataGridTextColumn绑定查询字段。
摘要由CSDN通过智能技术生成

使用MVVM模式做出平时的数据表格的数据查询,这和之前的WPF数据表格查询有所不同,因为使用的是MVVM模式,所以这个数据表格的数据来源也离不开Binding,通过Binding来绑定这个数据表格的数据源,首先我们需要设置一个属性,这个属性是一个list的集合,然后创建一个实体类CommodityDetailVo,让他继承你要查询的表的数据,其余的不是这个表的字段可以通过在CommodityDetailVo这个实体类里面添加那些字段的自属性来完成赋值,这个CommodityDetailVo实体类需要添加INotifyPropertyChanged这个接口来实现数据的更新

private List<CommodityDetailVo> _CommodityEntity;
        public List<CommodityDetailVo> CommodityEntity
        {
   
            get 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MVVM(Model-View-ViewModel)是一种常用的软件架构模式,其中ViewModel充当了Model和View之间的桥梁。在WPF应用程序中,MVVM是一种常用的设计模式。DataGrid是一种常用的WPF控件,用于显示数据。下面是一个MVVM模式下DataGrid绑定数据的代码示例: 首先,我们需要定义一个ViewModel类,该类应该包含一个属性,用于存储要显示在DataGrid中的数据。这个属性应该实现INotifyPropertyChanged接口,以便在数据更改时通知View更新。 ```csharp public class MyViewModel : INotifyPropertyChanged { private ObservableCollection<MyData> _myDataList; public ObservableCollection<MyData> MyDataList { get { return _myDataList; } set { _myDataList = value; OnPropertyChanged("MyDataList"); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } ``` 接下来,我们需要在View中创建一个DataGrid控件,并将其ItemsSource属性绑定到ViewModel中的MyDataList属性。 ```xaml <DataGrid ItemsSource="{Binding MyDataList}" AutoGenerateColumns="True"/> ``` 最后,在View的代码中,我们需要将ViewModel与View关联起来。 ```csharp public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // Create the ViewModel and set it as the DataContext for the View MyViewModel viewModel = new MyViewModel(); DataContext = viewModel; // Load the data into the ViewModel's MyDataList property viewModel.MyDataList = LoadMyData(); } private ObservableCollection<MyData> LoadMyData() { // Load the data from a data source // and return an ObservableCollection<MyData> object } } ``` 这样就完成了一个简单的MVVM模式下的DataGrid绑定数据。当ViewModel中的MyDataList属性更改时,DataGrid会自动更新以显示新数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值