【发布时间】: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