c# - 使用 MVVM、 Entity Framework 和 mysql 将基本数据绑定(bind)到文本框

标签 c# mysql wpf entity-framework mvvm

我刚刚开始使用 WPF 和 MVVM。我知道这个问题可能在 SO 上被问了很多,但我已经阅读了我能在 MVVM 和 EF 上找到的所有内容,尝试了那里的每个示例和教程,阅读了“如何在企业 MVVM 中生存”,但我仍然无法理解如何正确使用简单绑定(bind)到文本框的模式和框架。有人可以帮我举一个易于理解的例子吗?

我使用 EF Designer 创建了一个模型。在我的模型文件夹中,LocationModel.Context 如下所示:

型号:

namespace Location.Model
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class DailyEntities : DbContext
    {
        public DailyEntities()
            : base("name=DailyEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<LocationKPI> LocationKPI { get; set; }

    }
}

LocationKPI.cs

namespace Location.Model
{
    using System;
    using System.Collections.Generic;

    public partial class LocationKPI
    {
        public long sMonth { get; set; }
        public Nullable<decimal> Efficiency { get; set; }
    }
}

我创建了一个 ViewModel 文件夹,其中包含一个实现 INotifyPropertyChanged 的​​类:

View 模型:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Location.ViewModel
{
    public class LocationViewModel: INotifyPropertyChanged
    {

        public decimal Efficiency
        {
            get { return _Efficienecy; }
            set
            {
                _Efficiency = value;
                OnPropertyChanged("Efficiency");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

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

XAML:

<Window x:Class="Location.MainWindow"
        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:Location"
        xmlns:vm="clr-namespace:Location.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <DatePicker HorizontalAlignment="Left" Margin="392,17,0,0" VerticalAlignment="Top"/>
        <TextBlock Text="Efficiency"></TextBlock>
        <TextBox Text="{Binding Efficiency}" ></TextBox>

    </Grid>
</Window>

我真的在这里失去了理智。我正在寻找有关如何将模型和 View 模型“粘合”到我的 View 的一些指导,我只需要一个简单的示例即可理解。如果问题太宽泛,我很抱歉。

查询应该写在模型中还是 View 模型中?如何使用 linq 定义数据上下文并编写简单的 select Efficiency where sMonth = 9 语句?如何将日期选择器中的月份作为参数添加到上述查询中?如果有任何帮助,我将不胜感激。谢谢。

最佳答案

我将解决您的 MVVM 问题并将数据源留给另一个讨论。我将尝试为您提供一些良好的习惯,同时向您展示如何将 MVVM 环境连接在一起。

让我们从 INotifyPropertyChanged 接口(interface)以及如何实现它开始。一般来说,我尝试创建一个模型和 View 模型都使用的抽象类,其中包含我所有模型和 View 模型中共有的所有内容。这是那些抽象类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WpfStackOverflow.Models
{
/// <summary>
/// Abstract class that provides common functionality accross all View Models.
/// </summary>
public abstract class modelBase : INotifyPropertyChanged
{

    #region INotifyPropertyChanged
    /// <summary>
    /// Provides a simplefied INotifyPropertyChanged interface.
    /// requires System.Runtime.CompilerServices and
    /// System.ComponentModel.
    /// </summary>
    /// <param name="property"></param>
    public void SetPropertyChanged([CallerMemberName] string property = null)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}
#endregion
}

namespace WpfStackOverflow.ViewModels
{
    /// <summary>
    /// Abstract class that provides common functionality accross all View     Models.
    /// </summary>
    public abstract class ViewModelBase : INotifyPropertyChanged
    {

        #region INotifyPropertyChanged
        /// <summary>
        /// Provides a simplefied INotifyPropertyChanged interface.
        /// requires System.Runtime.CompilerServices and
        /// System.ComponentModel.
        /// </summary>
        /// <param name="property"></param>
        public void SetPropertyChanged([CallerMemberName] string property = null)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }
}

每个类分别位于 Models 和 ViewModels 文件夹中。 这是我创建的 View 模型:

namespace WpfStackOverflow.ViewModels
{
    class BasicMVVMViewModel:ViewModelBase
    {

        private ObservableCollectionEx<LocationKPI> _locations;

        public BasicMVVMViewModel()
        {
            CreateTestData();
        }

        private DateTime _selectedDate;
        public DateTime SelectedDate
        {

            get { return _selectedDate; }
            set
            {
                _selectedDate = value;
                SetPropertyChanged();
                SetSelectedLocationKPI( _selectedDate.Month);
            }
        }

        private LocationKPI _selectedLocationKPI;
        public LocationKPI SelectedLocationKPI
        {
            get { return _selectedLocationKPI; }
            set
            {
                _selectedLocationKPI = value;
                SetPropertyChanged();
            }
        }

        private void  SetSelectedLocationKPI(long sMonth)
        {
            SelectedLocationKPI = _locations.Where(p => p.SMonth.Equals(sMonth)).FirstOrDefault();
        }


        private void CreateTestData()
        {

            _locations = new ObservableCollectionEx<LocationKPI>();
            for(int i=0; i < 12;i++)
            {
                _locations.Add(new LocationKPI() { SMonth = i, Efficiency = i*17 });
            }

        }

    }
}

最后是 XAML 标记:

<UserControl x:Class="WpfStackOverflow.Controls.BasicMVVM"
             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:WpfStackOverflow.Controls"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="35"/>
            <RowDefinition Height="35"/>
            <RowDefinition Height="35"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <DatePicker HorizontalAlignment="Left" Margin="30,5,0,0"   Grid.Row="1" VerticalAlignment="Top" Width="150" SelectedDate="{Binding SelectedDate}"/>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Stretch"  Grid.Row="2" TextWrapping="Wrap" 
               Text="{Binding SelectedLocationKPI.Efficiency,Mode=TwoWay}" VerticalAlignment="Stretch" Margin="5"/>

    </Grid>
</UserControl>

在 Viewmodel 中,我调用了一个创建测试数据的方法。在您的情况下,您可以从 linq 查询中获取数据。 (你想通了:))

您必须调整一些引用和命名空间以适应您的环境。我在这里发布的内容已经过测试并且有效。我希望它能帮助你更好地理解 MVVM。哦,是的,还有一件事,您在此处看到的 XAML 来自 UserControl 而不是 XAML 窗口。在代码隐藏中,您必须实例化 ViewModel 并将其设置为 DataContext:

namespace WpfStackOverflow.Controls
{
    /// <summary>
    /// Interaction logic for BasicMVVM.xaml
    /// </summary>
    public partial class BasicMVVM : UserControl
    {
        public BasicMVVM()
        {
           InitializeComponent();
           DataContext = new BasicMVVMViewModel();
        }
    }
}

祝你好运。 StackOverflow 主要用于解决实际的编码问题。这个案例实际上是一个编码课。尽量避免在以后的类(class)中使用 StackOverflow。

关于c# - 使用 MVVM、 Entity Framework 和 mysql 将基本数据绑定(bind)到文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46474959/

相关文章:

c# - 将任务添加到 IObservable<T> (Finally 的对称性)

javascript - Jquery Ajax 上传大文件出错

c# - 使用 pinvoke 时,仅提供 dll 名称时 dll 的基本路径是什么?

c# - Code First 可选的一对一关系

wpf - 样式 TargetType 在未附加到调试器时会导致 XamlParseException

mysql - 不用 GroupConcat 将多行合并为一

Python 无法检查某个值是否在列表中

PHP:die() 必须死吗?

c# - 具有数据绑定(bind)和常量字符串的 WPF 窗口标题

.net - 在 DotNet RGB24 或 RGB32 中使用什么来提高性能