WPF中DatePiker值绑定以及精简查询
1.WPF中DatePiker值绑定
Xaml中值绑定使用Text
在ViewModel中使用strMinDate 转为DateTime.如下:
public string strMinDate
{
get { return this.GetProperty
set { this.SetProperty
MinDateTime = Convert.ToDateTime(value);
}
}
public string strMaxDate
{
get { return this.GetProperty
set { this.SetProperty
MaxDateTime = Convert.ToDateTime(value);
}
}
private DateTime? _minDateTime;
///
public DateTime? MinDateTime
{
get { return _minDateTime; }
set
{
_minDateTime = value;
this.RaisePropertyChanged(() => MinDateTime);
}
}
private DateTime? _maxDateTime;
///
public DateTime? MaxDateTime
{
get { return _maxDateTime; }
set
{
_maxDateTime = value;
this.RaisePropertyChanged(() => MaxDateTime);
}
}
2. Linq精简查询
public void Search()
{
DateTime tMin = _minDateTime == null ? DateTime.MinValue : _minDateTime.Value.Date;
DateTime tMax = _maxDateTime == null ? DateTime.MaxValue : _maxDateTime.Value.Date.AddDays(1);
IQueryable
this.Results = new ObservableCollection
}
3. 原代码:
Xaml:
1
ViewModel:
//------------------------------------------摘要------------------------------------------
// 产品名称:结果列表
// 文 件 名:ResultListViewModel
// 文件说明:
// 机器名称:EIBJDE0153
// 作 者:zhengping.pan
// 创建日期:2018-08-04 10:44:09
//----------------------------------------------------------------------------------------
using System;
using System.Linq;
using System.Data.Entity;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm;
using System.Collections.ObjectModel;
using EUROFluoAnalyzer.Data.Table;
using EUROFluoAnalyzer.Data;
using EUROFluoAnalyzer.Result.Views;
namespace EUROFluoAnalyzer.Result.ViewModels
{
[POCOViewModel]
public class ResultListViewModel : ViewModelBase
{
#region 属性
public string Barcode
{
get { return this.GetProperty
set { this.SetProperty
}
public string strMinDate
{
get { return this.GetProperty
set { this.SetProperty
MinDateTime = Convert.ToDateTime(value);
}
}
public string strMaxDate
{
get { return this.GetProperty
set { this.SetProperty
MaxDateTime = Convert.ToDateTime(value);
}
}
private DateTime? _minDateTime;
///
public DateTime? MinDateTime
{
get { return _minDateTime; }
set
{
_minDateTime = value;
this.RaisePropertyChanged(() => MinDateTime);
}
}
private DateTime? _maxDateTime;
///
public DateTime? MaxDateTime
{
get { return _maxDateTime; }
set
{
_maxDateTime = value;
this.RaisePropertyChanged(() => MaxDateTime);
}
}
public ObservableCollection
{
get { return GetProperty(() => Results); }
set { SetProperty(() => Results, value); }
}
public SampleResult SelectedResult
{
get { return this.GetProperty
set { this.SetProperty
}
private EuroLabContext _databaseContext;
#endregion
public ResultListViewModel()
{
Barcode = string.Empty;
strMaxDate = DateTime.Now.Date.ToString("yyyy-MM-dd");
strMinDate = new DateTime(DateTime.Now.Year,1,1).ToString("yyyy-MM-dd");
this._databaseContext = new EuroLabContext();
}
public void Search()
{
DateTime tMin = _minDateTime == null ? DateTime.MinValue : _minDateTime.Value.Date;
DateTime tMax = _maxDateTime == null ? DateTime.MaxValue : _maxDateTime.Value.Date.AddDays(1);
IQueryable
this.Results = new ObservableCollection
}
public void Detail()
{
if (this.SelectedResult != null)
{
IQueryable
ResultDetailView detailWindow = new ResultDetailView();
(detailWindow.DataContext as ResultDetailViewModel).SelectedResult = this.SelectedResult;
(detailWindow.DataContext as ResultDetailViewModel).Results = new ObservableCollection
detailWindow.ShowDialog();
}
}
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章