【问题标题】:WPF MVVM Binding Entity Frame Work Results to ComboboxWPF MVVM 将实体框架结果绑定到组合框
【发布时间】:2017-06-13 02:34:47
【问题描述】:

我正在实现一个 mvvm 模型。我的模型中有一个名为 Dep_Class 的类

using System.ComponentModel;

namespace mency.Models
{
    class Dep_class : INotifyPropertyChanged
    {
        private string dep_name;

        public string Dep_Name
        {
            get { return dep_name; }
            set
            {
                dep_name = value;
                OnPropertyChanged("Dep_Name");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public Dep_class()
        {

        }
        private void OnPropertyChanged(string propertyName)
        {
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }

        }

    }
}

我通过 EntityFramework 连接了数据库,这是从 EF 生成的类

namespace mency
{
    using System;
    using System.Collections.Generic;

    public partial class branch
    {
        public int Branch_ID { get; set; }
        public string Branch_Name { get; set; }
    }
}

在我的页面代码上我有 XAML

<Page x:Name="Departments_page" x:Class="mency.Department"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:mency"
      xmlns:m="clr-namespace:mency.Models"
      mc:Ignorable="d" 
      d:DesignHeight="502" d:DesignWidth="722"
      Title="Department" Initialized="Departments_page_Initialized" Loaded="Departments_page_Loaded">
    <Page.Resources>
        <m:Dep_class x:Key="dep_class" />

    </Page.Resources>

    <Grid Background="MediumAquamarine">

            <ComboBox x:Name="Branch_cbox" ItemsSource="{StaticResource dep_class}"  />



    </Grid>

如何将组合框绑定到我的数据库的加载结果。 这是我在页面后面的代码:

using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace mency
{
    /// <summary>
    /// Interaction logic for Department.xaml
    /// </summary>
    public partial class Department : Page
    {
        medical_databaseEntitiescon _dbObj;


        public Department()
        {


            InitializeComponent();


        }

        private void Departments_page_Initialized(object sender, EventArgs e)
        {

        }

        private void Departments_page_Loaded(object sender, RoutedEventArgs e)
        {
            _dbObj = new medical_databaseEntitiescon();
            //ComboBox = _dbObj.branches.ToList(); // I want combobox loaded with the results of this List
        }
    }
}

很抱歉问了这么长的问题,但我希望有人帮助我

【问题讨论】:

  • 如果你想实现 MVVM,我建议不要使用代码隐藏。 MVVM 字面意思是 Model-View-ViewModel,使用后面的代码是针对该框架的。至于链接,如果你真的想使用代码隐藏,不要在你的xaml中设置ItemSource={Binding ...,而是在Department()构造函数中使用Branch_cbox.ItemsSource = *list from database*,同时设置DataContext = this;
  • 好的,但是你能解释一下如何在没有代码隐藏的情况下使用它吗?
  • 您应该在模型中创建 medical_databaseEntitiescon 对象。在模型中编写一个返回 _dbObj.branches 的方法。在 viewModel 中创建模型的实例。还要在视图模型中创建一个 List 属性,在 getter 中调用模型的 _dbObj.branches 方法。将此 List 绑定到 UI 中的组合框。

标签: c# wpf entity-framework mvvm combobox


【解决方案1】:

在我的评论中,我建议不要将代码隐藏与 MVVM 结构一起使用。

我相信你会在网上找到大量的 MVVM 教程,但我会在这里给出一个简短的答案。

首先,您需要创建一个类 (ViewModel),将其命名为任何名称,并将其设置为您的 DataContext,以便您的视图(xaml)知道在哪里查找绑定,还将 ItemsSource="{StaticResource dep_class}" 更改为 ItemsSource="{Binding dep_class}"

<Page x:Name="Departments_page" x:Class="mency.Department"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:mency"
  xmlns:m="clr-namespace:mency.Models"
  mc:Ignorable="d" 
  d:DesignHeight="502" d:DesignWidth="722"
  Title="Department" Initialized="Departments_page_Initialized" Loaded="Departments_page_Loaded">
<Page.DataContext>
    <m:Dep_class *name of viewmodel class* />
</Page.DataContext>
<Grid Background="MediumAquamarine">
        <ComboBox ItemsSource="{Binding dep_List}" SelectedItem="{Binding SelectedDept, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>

然后,在您的班级中,您可以创建一个名为 dep_List 的列表/集合和一个名为 SelectedDept 的 Dep_class 变量。请记住在您的视图模型中实现INotifyPropertyChanged,以便它可以“监听”和“更新”视图的变化。最后,在您的视图模型中,您可以使用数据库中的值填充dep_List(如果需要,请使用DisplayMemberPath)。我知道它有很多东西可以吸收,但是一旦你学会了,它就会非常有趣且使用起来非常强大。我不会为你编写你的视图模型代码,因为那会是勺子,但是,这里有一些有用的链接:

MVVM Creating the ViewModel

Implementing the Model-View-ViewModel Pattern(有点难懂)

The World's Simplest C# WPF MVVM Example

【讨论】:

    【解决方案2】:

    我目前正在从事一个严重涉及 WPF 数据绑定与实体框架的项目。充分利用数据绑定为您提供的强大功能的唯一方法是创建包装数据库对象的包装类。这很烦人,但这是唯一的方法。

    我有一个名为 ObservableObjectBase 的基类,我的所有模型(包装器)都实现了它,它为我完成所有通知属性更改工作,然后确保我的 MVVM 框架正确使用它。

    没有固定的方法可以做到这一点,但我将简要解释一下我的解决方案:

    Presenter (DAL) - 我的 Presenter 类处理所有数据的读取和写入,并通过包装它们来转换数据库对象。当数据写入数据库时​​,该类还负责将包装器转换回对 Entity Framework 有意义的数据。

    Presentation Model (ViewModel) - 它存储所有包装对象并充当 UI 的 DataContext - 它不保留对任何实体框架对象的引用。

    这对某些人来说有点麻烦,直接绑定到数据库对象会很好,但不幸的是,这并不是一种奢侈。

    【讨论】:

      猜你喜欢
      • 2013-06-20
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 2015-07-11
      • 2015-04-27
      • 1970-01-01
      • 2013-10-24
      相关资源
      最近更新 更多