简体   繁体   English

数据绑定/设置源到我的DataGrid [WPF]的正确方法是什么

[英]What is proper way of data binding / setting source to my DataGrid [WPF]

I'm begginer at wpf so please be patient with me :) 我是wpf的乞讨人,所以请耐心等待我:)

I have stored 40.000 articles in MySql database, and when I click on a button I'm opening a window that loads that articles, and I did it on this way: 我已经在MySql数据库中存储了40.000条文章,当我单击一个按钮时,我正在打开一个加载该文章的窗口,我是通过以下方式完成的:

/// <summary>
/// Interaction logic
/// </summary>
public partial class ArticlesAdd : Window
{ 
   public ObservableCollection<MyArticles> articlesList = ObservableCollection<MyArticles>(ArticlesController.SelectAll());

   public ArticlesAdd()
   {
      this.InitializeComponent();

      // Setting source to my DATAGRID when this window is loaded/opened

      dataGridMyArticles.ItemsSource = articlesList;
    }
}

But I saw some examples are setting ItemsSource directly on DataGrid Control like this (IN XAML PART): 但是我看到一些示例直接在DataGrid控件上设置ItemsSource,如下所示(在XAML PART中):

<DataGrid Name="dataGridMyArticles" ItemsSource="{Binding Source=articlesList}"  AutoGenerateColumns="False">

But I don't know how this works and how this should be implemented because I'm using dataGridMyArticles.ItemsSource = articlesList; 但是我不知道它是如何工作的以及应该如何实现,因为我正在使用dataGridMyArticles.ItemsSource = articlesList;

Is that ItemsSource="{Binding Source=articlesList}" on a XAML side faster than my code behind binding ? XAML方面的ItemsSource="{Binding Source=articlesList}"是否比绑定背后的代码快?

and would it IsAsync=True make data binding faster/opens window faster or smth like that? IsAsync=True会使数据绑定更快/更快地打开窗口或像这样?

So how can I bind that list to my DataGrid without using code behind, and is that approach faster than setting DataGrid's source there in my Class constructor.. ? 因此,如何在不使用后面代码的情况下将该列表绑定到DataGrid ,并且这种方法是否比在Class构造函数中设置DataGrid's源速度更快。

Thanks guys Cheers 谢谢大家的欢呼

Binding an element in a view to a source property of a view model is common practice when you follow the Model-View-ViewModel (MVVM) design pattern . 当您遵循Model-View-ViewModel(MVVM) 设计模式时,通常将视图中的元素绑定到视图模型的source属性。 MVVM has nothing to do with performance but it is the recommended design pattern to use when developing XAML based UI applications. MVVM无关与性能,但它是开发基于XAML的UI应用程序时使用推荐设计模式。

It won't make your application faster, but if you do it right it will make the application easier to maintain, test and develop. 它不会使您的应用程序更快,但是,如果操作正确,它将使应用程序更易于维护,测试和开发。 You can read more about the motivations for implementing an application using MVVM pattern on MSDN: https://msdn.microsoft.com/en-us/library/hh848246.aspx . 您可以在MSDN上阅读有关使用MVVM模式实现应用程序的动机的更多信息: https : //msdn.microsoft.com/en-us/library/hh848246.aspx There are a lot more resources available online if your Google or Bing for it. 如果您的Google或必应Bing拥有大量在线资源。

In your particular example, you would define a view model class which holds the list of articles: 在您的特定示例中,您将定义一个包含文章列表的视图模型类:

public class ArticlesViewModel
{
    public ObservableCollection<MyArticles> ArticlesList { get; private set; }

    public ArticlesViewModel()
    {
        ArticlesList = ObservableCollection<MyArticles>(ArticlesController.SelectAll());
    }
}

Set the DataContext of the view to an instance of this class: 将视图的DataContext设置为此类的实例:

public partial class ArticlesAdd : Window
{
    public ArticlesAdd()
    {
        this.InitializeComponent();
        DataContext = new ArticlesViewModel();
    }
}

You can then bind to any public property of the DataContext /view model: 然后,您可以绑定到DataContext / view模型的任何公共属性

<DataGrid Name="dataGridMyArticles" ItemsSource="{Binding Source=ArticlesList}" AutoGenerateColumns="False">

You probably also want to call the ArticlesController.SelectAll() method on a background in order to prevent the UI from freezing during the time it takes to collect the data from the database, but that's another story that is not directly related to MVVM and the use of bindings. 您可能还想在后台调用ArticlesController.SelectAll()方法,以防止UI在从数据库收集数据的过程中冻结,但这是另一个与MVVM和使用绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM