将我的数据库中的数据绑定到 DataGrid WPF C# MVC 模式

问题描述 投票:0回答:1

我正在使用 EF Core 和 MVC 模式来创建桌面应用程序(我知道 MVVM 更好,但我被迫在工作中使用 MVC)。如何将数据直接从数据库链接到

Projets.xaml
用户控件中的数据网格。

这是表类

Projets.cs

using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.Windows.Controls;
using Projet.Views;

namespace Projet.Models
{
    public class Projets
    {
        [Key]
        public int ID_Projet { get; set; }

        [Required(ErrorMessage = "Le nom de de projet est obligatoire")]
        public string Nom_Projet { get; set; }

        public string Description_Projet { get; set; }

        [Required]
        public string Version_Projet { get; set; }

        [Required]
        public string Révision_Projet { get; set; }

        [Required(ErrorMessage = "La version de projet est obligatoire")]
        public string Statut_Projet { get; set; }

        //////
        public int ID_BDD { get; set; }
        public BDDs BDD { get; set; }
        //////   
        public int ID_Environnement { get; set; }
        public Environnements Environnement { get; set; }
        //////
        public int ID_Développeur { get; set; }
        public Développeurs Développeur { get; set; }
        //////
        public int ID_Languages { get; set; }
        public Languages Language { get; set; }
        //////
        public ICollection<ProjetsToolbox> ProjetToolbox { get; set; }
        //////
        public ICollection<ProjetsSites> ProjetSite { get; set; }
        //////
        public Plannings Planning { get; set; }
    }
}
c# wpf data-binding entity-framework-core
1个回答
0
投票

我认为最理想的是 MVC 和 MVVM 的某种组合。在本例中,您有一个 Projets.xaml 视图。该视图的主要数据源是数据上下文中定义的源对象。控制器从模型接收数据并将其传递到数据上下文。

概念示例:

namespace Projet.View
{
    /// <summary>
    /// Логика взаимодействия для Projets.xaml
    /// </summary>
    public partial class Projets : Window
    {

        public ProjetsData  Data { get =>(ProjetsData) DataContext; set => DataContext = value; }

        public Projets()
        {
            InitializeComponent();
        }
    }

    public class ProjetsData : ViewModelBase
    {
        public IEnumerable<Projets> Projets { get => Get<IEnumerable<Projets>>(); set => Set(value); }
    }

}
<Window x:Class="Projet.View.Projets"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Projet.View"
        mc:Ignorable="d"
        Title="Projets" Height="450" Width="800">
    <Window.DataContext>
        <local:ProjetsData/>
    </Window.DataContext>
    <Grid>
        <DataGrid ItemsSource="{Binding Projets}"/>
    </Grid>
</Window>
    public class SomeControler
    {
        private readonly Projet.Models.SomeModel model;

        private readonly Projet.View.Projets view;

        public void GetListProjets()
        {
            view.Data.Projets = model.GetProjets();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.