TwoWay 绑定不适用于 datepicker MVVM
TwoWay binding not working with datepicker MVVM
我终于设法开始使用 MVVM 并实施 INotifyPropertyChanged,但我面临以下问题:即使我将日期选择器中的月份作为参数传递给我的查询,我的查询结果却没有如果我选择不同的月份,则更改。
我希望 INotifyPropertyChanged 会解决这个问题。当从日期选择器中选择不同的月份时,如何确保我的效率发生变化?
谢谢
ViewModel
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"/>
您需要说明 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
设置为默认值。
它不起作用的原因是因为在您的 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));
}
}
}
我终于设法开始使用 MVVM 并实施 INotifyPropertyChanged,但我面临以下问题:即使我将日期选择器中的月份作为参数传递给我的查询,我的查询结果却没有如果我选择不同的月份,则更改。
我希望 INotifyPropertyChanged 会解决这个问题。当从日期选择器中选择不同的月份时,如何确保我的效率发生变化?
谢谢
ViewModel
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"/>
您需要说明 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
设置为默认值。
它不起作用的原因是因为在您的 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));
}
}
}