文档章节

WPF中DatePiker值绑定以及精简查询

o
 osc_4nmshwhm
发布于 2018/08/07 10:16
字数 937
阅读 0
收藏 0

精选30+云产品,助力企业轻松上云!>>>

WPF中DatePiker值绑定以及精简查询

1.WPF中DatePiker值绑定
Xaml中值绑定使用Text
<DatePicker Text="{Binding strMinDate,UpdateSourceTrigger=PropertyChanged, StringFormat=yyyy-MM-dd}" HorizontalAlignment="Left" Margin="69,2,0,0" VerticalAlignment="Top" Width="137" Height="30" FontSize="14" SelectedDateFormat="Long"/>
<DatePicker Text ="{Binding strMaxDate,UpdateSourceTrigger=PropertyChanged, StringFormat=yyyy-MM-dd}" HorizontalAlignment="Left" Margin="222,2,0,0" VerticalAlignment="Top" Width="134" Height="30" FontSize="14" SelectedDateFormat="Long"/>
 
在ViewModel中使用strMinDate 转为DateTime.如下:
public string strMinDate
{
get { return this.GetProperty<string>(() => strMinDate); }
set { this.SetProperty<string>(() => strMinDate, value);
MinDateTime = Convert.ToDateTime(value);
}
}
 
public string strMaxDate
{
get { return this.GetProperty<string>(() => strMaxDate); }
set { this.SetProperty<string>(() => strMaxDate, value);
MaxDateTime = Convert.ToDateTime(value);
}
}
 
 
private DateTime? _minDateTime;
/// <summary>
/// 起始时间
/// </summary>
public DateTime? MinDateTime
{
get { return _minDateTime; }
set
{
_minDateTime = value;
this.RaisePropertyChanged(() => MinDateTime);
}
}
private DateTime? _maxDateTime;
/// <summary>
/// 结束时间
/// </summary>
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<SampleResult> IResults = _databaseContext.SampleResults.Where(x => x.Sample.ArriveDateTime < tMax && x.Sample.ArriveDateTime >= tMin && x.Sample.Barcode.Contains(Barcode)).Include(p=>p.Sample).Include(p=>p.Sample.Test).Include(p=>p.Sample.Patient);
this.Results = new ObservableCollection<SampleResult>(IResults);
}

 

 

 
3. 原代码:
Xaml:
 1 <UserControl x:Class="EUROFluoAnalyzer.Result.Views.ResultListView"
 2  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 5  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 6  xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
 7  xmlns:ViewModels="clr-namespace:EUROFluoAnalyzer.Result.ViewModels"
 8  mc:Ignorable="d"
 9  DataContext="{dxmvvm:ViewModelSource Type=ViewModels:ResultListViewModel}" Height="725.373" Width="1020.746">
10  <Grid>
11  <Grid.RowDefinitions>
12  <RowDefinition Height="41"/>
13  <RowDefinition Height="45"/>
14  <RowDefinition/>
15  </Grid.RowDefinitions>
16  
17  <Label Content="送检日期" HorizontalAlignment="Left" Margin="3,2,0,0" VerticalAlignment="Top" Height="22" Width="61" FontSize="14"/>
18  <DatePicker Text="{Binding strMinDate,UpdateSourceTrigger=PropertyChanged, StringFormat=yyyy-MM-dd}" HorizontalAlignment="Left" Margin="69,2,0,0" VerticalAlignment="Top" Width="137" Height="30" FontSize="14" SelectedDateFormat="Long"/>
19  <Label Content="-" HorizontalAlignment="Left" Margin="212,4,0,0" VerticalAlignment="Top" Height="22" Width="6" FontSize="14"/>
20  <DatePicker Text ="{Binding strMaxDate,UpdateSourceTrigger=PropertyChanged, StringFormat=yyyy-MM-dd}" HorizontalAlignment="Left" Margin="222,2,0,0" VerticalAlignment="Top" Width="134" Height="30" FontSize="14" SelectedDateFormat="Long"/>
21  <Label Content="样本编号" HorizontalAlignment="Left" Margin="376,4,0,0" VerticalAlignment="Top" Height="22" Width="61" FontSize="14"/>
22  <TextBox HorizontalAlignment="Left" Height="30" Margin="442,2,0,0" TextWrapping="Wrap" Text="{Binding Barcode}" VerticalAlignment="Top" Width="119" FontSize="14"/>
23  <Button Content="查询" Command="{Binding SearchCommand}" HorizontalAlignment="Left" Margin="576,2,0,0" VerticalAlignment="Top" Width="111" Height="32" FontSize="14"/>
24  <Grid Grid.Row="1">
25  <Button Content="详细" Command="{Binding DetailCommand}" HorizontalAlignment="Left" Margin="0,3,0,0" VerticalAlignment="Top" Width="111" Height="32" FontSize="14" />
26  </Grid>
27  <Grid Grid.Row="2">
28  <DataGrid ItemsSource="{Binding Results, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,5,0,0" IsReadOnly="True" AutoGenerateColumns="False" GridLinesVisibility="All"
29  x:Name="dataGrid"
30  SelectedItem="{Binding SelectedResult}" BorderThickness="1" HorizontalGridLinesBrush="#FFAAA5A5" VerticalGridLinesBrush="#FF9C9999" AlternatingRowBackground="#FFAEF1A7" FontSize="14">
31  <DataGrid.Columns>
32  <DataGridTemplateColumn Width="20">
33  <DataGridTemplateColumn.HeaderTemplate>
34  <DataTemplate>
35  <Grid>
36  <Border Height="18" Width="18" CornerRadius="2" Background="White" Padding="0" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
37  </Grid>
38  
39  </DataTemplate>
40  </DataGridTemplateColumn.HeaderTemplate>
41  </DataGridTemplateColumn>
42  <DataGridTextColumn Header="样本编号" Width="100" Binding="{Binding Sample.Barcode}"/>
43  <DataGridTextColumn Header="样本架" Width="50" Binding="{Binding Sample.Rack}"/>
44  <DataGridTextColumn Header="管位" Width="50" Binding="{Binding Sample.Position}"/>
45  <DataGridTextColumn Header="检测项" Width="100" Binding="{Binding Sample.Test.LongName}"/>
46  <DataGridTextColumn Header="基质编号" Width="100" Binding="{Binding Order}"/>
47  <DataGridTextColumn Header="细胞核型" Width="100" Binding="{Binding CellType}"/>
48  <DataGridTextColumn Header="滴度" Width="100" Binding="{Binding Titer}"/>
49  <DataGridTextColumn Header="结果" Width="100" Binding="{Binding Result}"/>
50  <DataGridTextColumn Header="送检时间" Width="150" Binding="{Binding Sample.ArriveDateTime, StringFormat=yyyy-MM-dd HH:mm:ss}" />
51  </DataGrid.Columns>
52  </DataGrid>
53  </Grid>
54  </Grid>
55 </UserControl>

 

 

 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<string>(() => Barcode); }
 set { this.SetProperty<string>(() => Barcode, value); }
 }
 
 public string strMinDate
 {
 get { return this.GetProperty<string>(() => strMinDate); }
 set { this.SetProperty<string>(() => strMinDate, value);
 MinDateTime = Convert.ToDateTime(value);
 }
 }
 
 public string strMaxDate
 {
 get { return this.GetProperty<string>(() => strMaxDate); }
 set { this.SetProperty<string>(() => strMaxDate, value);
 MaxDateTime = Convert.ToDateTime(value);
 }
 }
 
 
 private DateTime? _minDateTime;
 /// <summary>
 /// 起始时间
 /// </summary>
 public DateTime? MinDateTime
 {
 get { return _minDateTime; }
 set
 {
 _minDateTime = value;
 this.RaisePropertyChanged(() => MinDateTime);
 }
 }
 private DateTime? _maxDateTime;
 /// <summary>
 /// 结束时间
 /// </summary>
 public DateTime? MaxDateTime
 {
 get { return _maxDateTime; }
 set
 {
 _maxDateTime = value;
 this.RaisePropertyChanged(() => MaxDateTime);
 }
 }
 
 public ObservableCollection<SampleResult> Results
 {
 get { return GetProperty(() => Results); }
 set { SetProperty(() => Results, value); }
 }
 
 public SampleResult SelectedResult
 {
 get { return this.GetProperty<SampleResult>(() => SelectedResult); }
 set { this.SetProperty<SampleResult>(() => SelectedResult, value); }
 }
 
 
 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<SampleResult> IResults = _databaseContext.SampleResults.Where(x => x.Sample.ArriveDateTime < tMax && x.Sample.ArriveDateTime >= tMin && x.Sample.Barcode.Contains(Barcode)).Include(p=>p.Sample).Include(p=>p.Sample.Test).Include(p=>p.Sample.Patient);
 this.Results = new ObservableCollection<SampleResult>(IResults);
 }
 
 public void Detail()
 {
 if (this.SelectedResult != null)
 {
 IQueryable<SampleResult> IResults = _databaseContext.SampleResults.Where(x => x.Sample_ID == SelectedResult.Sample.ID && x.Sample.Protocol_ID == SelectedResult.Sample.Protocol_ID).Include(p => p.Sample).Include(p => p.Sample.Test).Include(p => p.Sample.Patient);
 ResultDetailView detailWindow = new ResultDetailView();
 (detailWindow.DataContext as ResultDetailViewModel).SelectedResult = this.SelectedResult;
 (detailWindow.DataContext as ResultDetailViewModel).Results = new ObservableCollection<SampleResult>(IResults);
 detailWindow.ShowDialog();
 }
 }
 
 }
}

 

上一篇: Partial关键字
o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

如何将div放置在其容器的底部? - How can I position my div at the bottom of its container?

问题: Given the following HTML: 鉴于以下HTML: <div id="container"> <!-- Other elements here --> <div id="copyright"> Copyright Foo web designs </div> </div> I would like #cop......

富含淀粉
27分钟前
10
0
unity列表控件Horizontal/Vertical/Grid Layout Group用法介绍

1. Grid Layout Group 为Panel控件添加Grid Layout Group,子控件为四个按钮,分别为Grid,Calendar,Gear,User: 默认属性为 为方便演示,按钮的底色为控件自带image,按钮上面的图标为其子...

路过暴风
52分钟前
19
0
Distinct()与lambda? - Distinct() with lambda?

问题: Right, so I have an enumerable and wish to get distinct values from it. 是的,所以我有一个可枚举的,并希望从中获得不同的值。 Using System.Linq , there's of course an ext......

法国红酒甜
57分钟前
8
0
学习编写编译器[关闭] - Learning to write a compiler [closed]

问题: Preferred languages : C/C++, Java, and Ruby. 首选语言 :C / C ++,Java和Ruby。 I am looking for some helpful books/tutorials on how to write your own compiler simply for......

技术盛宴
今天
17
0
OSChina 周一乱弹 —— 毛巾又怎么样?!我在乎的是大姐姐温柔的怀抱!

Osc乱弹歌单(2020)请戳(这里) 【今日歌曲】 @薛定谔的兄弟 :分享洛神有语创建的歌单「我喜欢的音乐」: 《雨 因你而下,于你而止》- Seto 手机党少年们想听歌,请使劲儿戳(这里) @Dan...

小小编辑
今天
43
1

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部