我终于设法开始使用MVVM并实现了INotifyPropertyChanged并且我遇到了以下问题:即使我将我的datepicker中的月份作为参数传递给我的查询,我的查询结果也没有&#39 ;如果我选择不同的月份,我会改变。
我希望INotifyPropertyChanged可以解决这个问题。从datepicker中选择不同的月份时,如何确保效率发生变化?
谢谢
视图模型
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"/>
答案 0 :(得分:1)
当Efficiency
MDate
必须更新
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
设置为构造函数中的默认值。
答案 1 :(得分: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));
}
}
}