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();
}
} }
}

WPF中DatePiker值绑定以及精简查询的更多相关文章

  1. WPF中,如何将绑定源设置到单件实例

    原文:WPF中,如何将绑定源设置到单件实例  WPF中,如何将绑定源设置到单件实例                                       周银辉 大概两个月前,曾有位朋友问我:如 ...

  2. 总结:WPF中模板需要绑定父级别的ViewModel该如何处理

    原文:总结:WPF中模板需要绑定父级别的ViewModel该如何处理 <ListBox ItemsSource="{Binding ClassCollection}"> ...

  3. 正确处理WPF中Slider值改变事件的方式

    最近在用WPF数据绑定重写一下播放器项目时遇到的关于Slider的问题,在窗体透明度调节和播放进度调节上用了Slider控件.调节窗体透明度我是 这么想的:将窗体的Opacity属性的值与Slider ...

  4. WPF实现多值绑定特性以及多值转换

    WPF中的实现 我们首先来看一下常规的绑定 <Window    x:Class="WpfApplicationSample.MainWindow"    xmlns=&qu ...

  5. WPF中ListBox的绑定

    WPF中列表式控件派生自ItemsControl类,继承了ItemsSource属性.ItemsSource属性可以接收一个IEnumerable接口派生类的实例作为自己的值(所有可被迭代遍历的集合都 ...

  6. wpf中的datagrid绑定操作按钮是否显示或者隐藏

    如图,需要在wpf中的datagrid的操作那列有个确认按钮,然后在某些条件下确认按钮可见,某些情况下不可见的,放在mvc里直接在cshtml页面中if..else就行了. 但是在wpf里不行..网上 ...

  7. WPF中使用ObjectDataProvider绑定方法

    ObjectDataProvider提供了绑定任意.net类型的功能,具体功能如下: 1.ObjectDataProvider提供了绑定任意CLR类型的公嫩那个. 2.它可以再XAML中利用生命史的语 ...

  8. wpf 中的DataTemplate 绑定控件

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  9. 【WPF】WPF中的List<T>和ObservableCollection<T>

    在WPF中 控件绑定数据源时,数据源建议采用 ObservableCollection<T>集合 ObservableCollection<T> 类:表示一个动态数据集合,在添 ...

随机推荐

  1. binary hacks读数笔记(堆、栈 VMA的分布)

    一.首先看一个简单的程序: #include<stdlib.h> int main() { while(1) { sleep(1000); } return 0; } gcc -stati ...

  2. 集成学习算法——adaboost

    adaboost是boosting类集成学习方法中的一种算法,全称是adaptive boost,表示其是一种具有自适应性的算法,这个自适应性体现在何处,下面来详细说明. 1.adaboost算法原理 ...

  3. FLEX弹性布局小结

    1. 弹性盒子中: flex: 0 1 auto表示什么意思 flex默认三个参数是flex-grow, flex-shrink, flex-basis,默认值是0 1 auto. flex-grow ...

  4. Django实战总结 - 快速开发一个数据库查询工具

    一.简介 Django 是一个开放源代码的 Web 应用框架,由 Python 写成. Django 只要很少的代码就可以轻松地完成一个正式网站所需要的大部分内容,并进一步开发出全功能的 Web 服务 ...

  5. ssh 方面问题总结

    ssh 远程执行命令: https://www.cnblogs.com/youngerger/p/9104144.html ssh免密登录: https://blog.csdn.net/jeikerx ...

  6. [web安全原理]PHP命令执行漏洞基础

    前言 PHP命令执行漏洞 应用程序的某些功能功能需要调用可以执行系统命令的函数,如果这些函数或者函数的参数被用户控制,就有可能通过命令连接符将恶意命令拼接到正常的函数中,从而随意执行系统命令,这就是命 ...

  7. 防sql注入函数

  8. 从执行上下文角度重新理解.NET(Core)的多线程编程[1]:基于调用链的”参数”传递

    线程是操作系统能够进行运算调度的最小单位,操作系统线程进一步被封装成托管的Thread对象,手工创建并管理Thread对象已经成为了所能做到的对线程最细粒度的控制了.后来我们有了ThreadPool, ...

  9. 【GIT】命令笔记

    1.将本地代码提交到github等仓库 1.创建仓库省略 2.切换到本地需要上传的地址 :初始化仓库 git init 3.配置git,告诉git你是谁 git config --global use ...

  10. Kafka 内存管理类BufferPool

    基本上每个成熟的框架或者工具都有一套内存管理机制 BufferPool 是Kafka  用来管理内存的工具类         BufferPool内存管理包含2个部分,已用空间+可用空间(未申请空间+ ...