【问题标题】:TwoWay binding not working with datepicker MVVM双向绑定不适用于日期选择器 MVVM
【发布时间】:2017-09-30 00:43:18
【问题描述】:

我终于设法开始使用 MVVM 并实现 INotifyPropertyChanged,我面临以下问题:即使我将日期选择器中的月份作为参数传递给我的查询,如果我的查询结果不会改变我选择了不同的月份。

我希望 INotifyPropertyChanged 能解决这个问题。从日期选择器中选择不同月份时,如何确保我的效率发生变化?

谢谢

视图模型

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Location.Model;
using System.Windows.Controls;

namespace Location.ViewModel
{
    public class LocationViewModel: INotifyPropertyChanged
    {


        public LocationViewModel()
        {
            var month = 0;
            int.TryParse(MDate.ToString("MM"), out month);
            var db = new DailyEntities();
            Efficiency = Convert.ToDecimal(db.LocationKPI.Where(a => a.sMonth == month).Select(a => a.Efficiency).FirstOrDefault());
        }

        private DateTime _mDate = DateTime.Now;

        public DateTime MDate
        {
            get { return _mDate; }
            set { _mDate = value; OnPropertyChanged("MDate"); }
        }

        decimal efficiency;

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


        public event PropertyChangedEventHandler PropertyChanged;

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

查看

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new LocationViewModel();
    }
}

XAML

<DatePicker x:Name="vDatePick" SelectedDateChanged="vDatePick_SelectedDateChanged" SelectedDate="{Binding MDate, Mode=TwoWay}" Text="Pick date"/>

【问题讨论】:

  • 您在 MVVM 应用程序中有代码? SelectedDateChanged="vDatePick_SelectedDateChanged"
  • 你是对的,这是非常糟糕的做法。我刚刚开始过渡到 MVVM,只是剩下的。

标签: c# wpf mvvm datepicker entity


【解决方案1】:

它不起作用的原因是因为在您的 ViewModel 中,您只是在执行代码以在构造函数中设置效率。您需要创建一个方法,然后从构造函数和 MDate 集合中调用它,因此每次 MDate 更改时都会更新效率:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Location.Model;
using System.Windows.Controls;

namespace Location.ViewModel
{
    public class LocationViewModel: INotifyPropertyChanged
    {
        public LocationViewModel()
        {
            SetEfficiency();
        }

        private DateTime _mDate = DateTime.Now;

        public DateTime MDate
        {
            get { return _mDate; }
            set 
            {
                _mDate = value;
                OnPropertyChanged("MDate");
                SetEfficiency();
            }
        }

        decimal efficiency;

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

        DailyEntities db = new DailyEntities();

        private void SetEfficiency()
        {
            var month;
            int.TryParse(MDate.ToString("MM"), out month);
            Efficiency = Convert.ToDecimal(db.LocationKPI.Where(a => a.sMonth == month).Select(a => a.Efficiency).FirstOrDefault());
        }

        public event PropertyChangedEventHandler PropertyChanged;

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

【讨论】:

  • DSway,向你致敬!这正是我一直在寻找的:一个完整​​、直接且易于理解的示例。你救了我的周末,我欠你一个冷的。太感谢了!你是男人!!!
【解决方案2】:

当MDate 是时,您需要声明Efficiency 必须更新

public DateTime MDate
{
   get { return _mDate; }
   set 
   { 
       _mDate = value; 
       OnPropertyChanged("MDate"); 
       OnPropertyChanged("Efficiency"); 
   }
}

我还将Efficiency 的查询放在getter 中并删除setter。这反过来意味着您不再需要私有变量 efficiency 并且您将删除构造函数中的查询。

public decimal Efficiency
{
    get 
    {
        var month = 0;
        int.TryParse(MDate.ToString("MM"), out month);
        return Convert.ToDecimal(
            db.LocationKPI
                .Where(a => a.sMonth == month)
                .Select(a => a.Efficiency)
                .FirstOrDefault());
    }
}

编辑:更新 Efficiency getter 和 setter

编辑:需要注意的是,如果您想在页面加载时查看结果,您也应该在构造函数中将 MDate 设置为默认值。

【讨论】:

  • 非常感谢大卫!作为一个菜鸟,我从你的例子中学到了很多。我真的很感激你的时间。周末愉快!
猜你喜欢
  • 2015-12-15
  • 2016-02-11
  • 2012-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多