如何使用WPF和MVVM从数据库中加载DataGrid数据? [英] How do I load a DataGrid with data from a database using WPF and MVVM?

查看:480
本文介绍了如何使用WPF和MVVM从数据库中加载DataGrid数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个完整的新手WPF和MVVM,所以我事先道歉,如果这个查询是很简单。我在网上搜索,没有找到任何符合我的要求。 Hense为什么我在这里!

I am a complete newbie to WPF and MVVM so I apologise in advance if this query is quite simple. I've searched online and havent been able to find anything which fits my requirements. Hense why I'm here!

我目前正在尝试使用LINQ实现从数据库查询的数据表。这是我运行的查询:

I am currently trying to implement a table of data queried from a database using LINQ. This is the query I run:

DataContext connection = new DataContext();

    var getTripInformation = from m in connection.tblTrips
                where m.TripDate > DateTime.Today
                select new { m.TripID, m.TripName, m.TripDate, m.ClosingDate, m.PricePerAdult, m.PricePerChild, m.Status };

这会填充我的var与我期望的相关信息。

Which fills my var with the relevant information which I expect.

现在,我想要做的是使用DataGrid在我的视图中显示它。任何人都可以帮助我这个?

Now, what I want to be able to do is diplay this in my View using a DataGrid. Can anyone assist me with this?

推荐答案

简而言之,你会有你的View和ViewModel。 ViewModel将需要实现INotifyPropertyChanged接口以方便视图绑定。这只是提供了一个事件,当您更改ViewModel上的属性时引发。然后,您的视图将绑定到ViewModel的属性。只要视图的DataContext被设置为ViewModel实例,这就工作。下面,这是在代码隐藏,但许多纯粹主义者直接在XAML中这样做。一旦这些关系被定义,运行您的LINQ查询填充ObservableCollection(它也实现INotifyPropertyChanged当项目被添加/删除内部)和您的网格将显示数据。

In a nutshell, you will have your View and ViewModel. The ViewModel will need to implement the INotifyPropertyChanged interface to facilitate view binding. This just provides an event that is raised when you change a property on your ViewModel. Your View will then bind to the ViewModel's properties. This works as long as the DataContext of the view is set to a ViewModel instance. Below, this is done in code-behind, but many purists do this directly in XAML. Once these relationships are defined, run your LINQ query to populate the ObservableCollection (which also implements INotifyPropertyChanged for when items are added/removed internally) and your grid will show the data.

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private ObservableCollection<MyRecord> _records = null;
    public ObservableCollection<MyRecord> Records 
    {
        get { return _records; }
        set
        {
            if( _records != value )
            {
                _records = value;

                if( this.PropertyChanged != null )
                {
                    this.PropertyChanged( this, new PropertyChangedEventArgs( "Records" ) );
                }
             }
         }
    }

    public MyViewModel()
    {
        this.Records = new ObservableCollection<MyRecord>();
        this.LoadData();
    }

    private void LoadData()
    {
        // this populates Records using your LINQ query
    }



检视(代码背后)



View (Code-Behind)

public class MyView : UserControl
{
    public MyView()
    {
        InitializeControl();

        // setup datacontext - this can be done directly in XAML as well
        this.DataContext = new MyViewModel();
    }
}



查看(XAML)



View (XAML)

<DataGrid
    ItemsSource="{Binding Path=Records, Mode=OneWay}"
    ...
/>



如果您设置 AutoGenerateColumns ='True'在DataGrid上,它将为绑定项类型的每个公共属性创建一行。如果将此值设置为false,则需要显式列出列和它们将映射到的属性。

If you set AutoGenerateColumns = 'True' on your DataGrid, it will create a row for each public property of the bound item type. If you set this value to false, you will need to explicitly list the columns and what property they will map to.

这篇关于如何使用WPF和MVVM从数据库中加载DataGrid数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆