2019年01月19日 22:40
原创作品,转载时请务必以超链接形式标明文章原始出处,否则将追究法律责任。
本节我们就看一下常见病维护界面,废话不多说,看一下界面。
很简单的一个CRUD,U在哪,U是双击出来的。
勾选一条或者多条点击删除,弹出confirm。
点击新增,弹出新增界面。
对于没有输入的边框背景为红色,文本框获得焦点以后,显示提示语。
OK,我们看一下页面及cs代码。
<Base:WindowBase x:Class="HealthyInfomation.Windows.CommonDisease" xmlns:Base="clr-namespace:HealthyInformation.FrameWork;assembly=HealthyInformation.FrameWork" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:HealthyInfomation.Resource" ResizeMode="NoResize" Title="常见病管理" Height="550" WindowStartupLocation="CenterScreen"> <Base:WindowBase.Resources> <Style TargetType="DataGrid"> <Setter Property="Margin" Value="0,5,0,0"></Setter> <Setter Property="Height" Value="410"></Setter> </Style> </Base:WindowBase.Resources> <Grid Margin="5"> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"></ColumnDefinition> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.ColumnSpan="2"> <TextBlock Text="{x:Static local:CommonDiseaseResource.Lab_DiseaseName}" VerticalAlignment="Center" Grid.Row="0"></TextBlock> <TextBox Text="{Binding DiseaseName,Mode=TwoWay}" Width="300"></TextBox> <Button Content="{x:Static local:CommonResource.Btn_Search}" Style="{DynamicResource btn-primary}" Margin="10,0,0,0" Command="{Binding SearchCommand}"></Button> </StackPanel> <DataGrid x:Name="DG_CommonDisease" Grid.Row="1" AutoGenerateColumns="False" Grid.ColumnSpan="2" Margin="0,5,0,1" CanUserAddRows="False" CanUserDeleteRows="False" Cursor="Hand" AlternatingRowBackground="#CCCCCC" SelectedItem="{Binding CommonDiseaseModel.SelectedCommonDisease,Mode=TwoWay}" ItemsSource="{Binding CommonDiseaseModel.CommonDiseaseList}"> <DataGrid.Columns> <DataGridTemplateColumn Header="操作" Width="40"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></CheckBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Header="{x:Static local:CommonDiseaseResource.DgHeader_DiseaseName}" Binding="{Binding SymptomName, Mode=OneWay}" MinWidth="100" MaxWidth="100"/> <DataGridTextColumn Header="{x:Static local:CommonDiseaseResource.DgHeader_DiseaseStatus}" Binding="{Binding SymptomDetail, Mode=OneWay}" MinWidth="300" MaxWidth="300"/> <DataGridTextColumn Header="{x:Static local:CommonDiseaseResource.DgHeader_Medication}" Binding="{Binding Medication, Mode=OneWay}" MinWidth="200" MaxWidth="200"/> <DataGridTextColumn Header="{x:Static local:CommonDiseaseResource.DgHeader_Treatment}" Binding="{Binding TreatmentPlan,Mode=OneWay}" MinWidth="300" MaxWidth="300"/> </DataGrid.Columns> </DataGrid> <Grid Grid.Row="2" Grid.ColumnSpan="2" Margin="0,10,0,0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <Button Command="{Binding NewCommand}" Content="{x:Static local:CommonResource.Btn_New}" Style="{DynamicResource btn-primary}" Grid.Column="0"></Button> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="1"> <Button Command="{Binding RemoveCommand}" Content="{x:Static local:CommonResource.Btn_Remove}" Style="{DynamicResource btn-danger}"></Button> <Button Command="{Binding CloseCommand}" Content="{x:Static local:CommonResource.Btn_Close}" Style="{DynamicResource btn-warning}" Margin="5,0,0,0"></Button> </StackPanel> </Grid> </Grid> </Base:WindowBase>
上面是页面代码,非常easy,哥不想多说,如果不懂就去看上一篇登录。
我们看一下cs代码。
public partial class CommonDisease : WindowBase { DispatcherTimer doubleClickTimer; CommonDiseaseFacade commonDiseaseFacade; public CommonDisease() { InitializeComponent(); this.commonDiseaseFacade = new CommonDiseaseFacade(this); this.CommonDiseaseModel = new CommonDiseaseSearchModel(); this.DataContext = this; this.InitTriggerAction(); this.InitTimer(); this.Loaded += delegate(object sender, RoutedEventArgs e) { this.SearchDisease(); }; } #region ViewModel private CommonDiseaseSearchModel commonDiseaseModel; public CommonDiseaseSearchModel CommonDiseaseModel { get { return commonDiseaseModel; } set { commonDiseaseModel = value; RaisePropertyChanged("CommonDiseaseModel"); } } public ICommand SearchCommand { get { return CommandFactory.CreateCommand((obj) => { this.SearchDisease(); }); } } public ICommand CloseCommand { get { return CommandFactory.CreateCommand((obj) => { this.Close(); }); } } public ICommand NewCommand { get { return CommandFactory.CreateCommand((obj) => { this.NewCommonDisease(); }); } } public ICommand RemoveCommand { get { return CommandFactory.CreateCommand(async (obj) => { await this.RemoveCommonDisease(); }); } } #endregion #region method private void InitTimer() { doubleClickTimer = new DispatcherTimer(); doubleClickTimer.Interval = new TimeSpan(0, 0, 0, 0, 400); doubleClickTimer.Tick += new EventHandler(DoubleClick_Timer); } private void DoubleClick_Timer(object sender, EventArgs e) { doubleClickTimer.Stop(); } private void InitTriggerAction() { this.DG_CommonDisease.AttchEventTriggerAction(new BaseTrigger<DependencyObject, CommonDisease>((obj) => { if (this.CommonDiseaseModel.SelectedCommonDisease == null) return; if (doubleClickTimer.IsEnabled) { doubleClickTimer.Stop(); var commonDiseaseUpdate = new CommonDiseaseUpdate(CommonDiseaseModel.SelectedCommonDisease); if (commonDiseaseUpdate.ShowDialog().GetValueOrDefault(false)) { this.SearchDisease(); } } else { doubleClickTimer.Start(); } }), EventEnums.MouseLeftButtonDown.ToString()); } private async void SearchDisease(int pageIndex = 0, int pageSize = 10) { var result = await this.commonDiseaseFacade.GetCommonDiseaseList(CommonDiseaseModel.DiseaseName, pageIndex, pageSize); this.CommonDiseaseModel.CommonDiseaseList = new ObservableCollection<CommonDiseaseEntity>(result.CommonDiseaseList); this.CommonDiseaseModel.TotalCount = result.TotalCount; } private void NewCommonDisease() { var commonDiseaseCreate = new CommonDiseaseCreate(); if (commonDiseaseCreate.ShowDialog().GetValueOrDefault(false)) { this.SearchDisease(); } } private async Task RemoveCommonDisease() { if (this.CommonDiseaseModel.CommonDiseaseList == null || this.CommonDiseaseModel.CommonDiseaseList.Count == 0) return; var removeCommonDiseaseList = this.CommonDiseaseModel.CommonDiseaseList.Where(c => c.IsChecked).ToList(); if (removeCommonDiseaseList.Count == 0) { this.ShowWarning(CommonDiseaseResource.Msg_NoCheckedRemove); return; } if (this.ShowConfirm(CommonDiseaseResource.Msg_RemoveConfirm) != MessageBoxResult.Yes) return; foreach (var commonDisease in removeCommonDiseaseList) { await this.commonDiseaseFacade.RemoveCommonDisease(commonDisease.TransactionNumber); } this.ShowMessage(CommonMsgResource.Msg_RemoveSuccess); this.SearchDisease(); } #endregion }
在这里双击弹出修改界面,其实在WPF中,没有提供DataGrid的双击事件,所以我们就定义一个timer,执行间隔时间为400ms。所以当我们点击DataGrid的时候,第一次点击会启动timer,第二次点击判断如果timer还时enabled状态就说明两次点击间隔时间小于400ms。就是双击,否则就是单击,结合InitTriggerAction方法你会理解的。
这就是修改界面,BootStrap风格,看起来还不错。最后我们看一下ViewModel的验证。
这个是新增界面,看一下代码。
<Base:WindowBase x:Class="HealthyInfomation.Windows.UserControl.CommonDiseaseCreate" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:Base="clr-namespace:HealthyInformation.FrameWork;assembly=HealthyInformation.FrameWork" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:HealthyInfomation.Resource" WindowStartupLocation="CenterScreen" Title="常见病新增" Height="440" Width="700" ResizeMode="NoResize"> <Grid Margin="5"> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <TextBlock Text="{x:Static local:CommonDiseaseResource.Lab_DiseaseName}" Grid.Row="0" Grid.Column="0"></TextBlock> <TextBox Text="{Binding CommonDiseaseCreateModel.SymptomName,Mode=TwoWay,ValidatesOnNotifyDataErrors=True,ValidatesOnDataErrors=True, NotifyOnValidationError=True}" MaxLength="100" Grid.Row="0" Grid.Column="1"></TextBox> <TextBlock Text="{x:Static local:CommonDiseaseResource.Lab_DiseaseStatus}" Grid.Row="1" Grid.Column="0" Margin="0,5"></TextBlock> <TextBox Text="{Binding CommonDiseaseCreateModel.SymptomDetail,Mode=TwoWay,ValidatesOnDataErrors=True, NotifyOnValidationError=True}" AcceptsReturn="True" Height="80" MaxLength="1000" Grid.Row="1" Grid.Column="1" Margin="0,5" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"></TextBox> <TextBlock Text="{x:Static local:CommonDiseaseResource.Lab_Medication}" Grid.Row="2" Grid.Column="0" Margin="0,5"></TextBlock> <TextBox Text="{Binding CommonDiseaseCreateModel.Medication,Mode=TwoWay,ValidatesOnDataErrors=True, NotifyOnValidationError=True}" AcceptsReturn="True" Height="80" MaxLength="1000" Grid.Row="2" Grid.Column="1" Margin="0,5" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"></TextBox> <TextBlock Text="{x:Static local:CommonDiseaseResource.Lab_Treatment}" Grid.Row="3" Grid.Column="0" Margin="0,5"></TextBlock> <TextBox Text="{Binding CommonDiseaseCreateModel.TreatmentPlan,Mode=TwoWay,ValidatesOnDataErrors=True, NotifyOnValidationError=True}" AcceptsReturn="True" Height="100" MaxLength="1000" Grid.Row="3" Grid.Column="1" Margin="0,5" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"></TextBox> <StackPanel Orientation="Horizontal" Grid.Row="4" Grid.ColumnSpan="2" Margin="0,10" HorizontalAlignment="Right"> <Button Command="{Binding SaveCommand}" Content="{x:Static local:CommonResource.Btn_Save}" Style="{DynamicResource btn-primary}"></Button> <Button Command="{Binding CloseCommand}" Content="{x:Static local:CommonResource.Btn_Close}" Style="{DynamicResource btn-warning}" Margin="5,0,0,0"></Button> </StackPanel> </Grid> </Base:WindowBase>
注意这里的绑定属性都有ValidatesOnDataErrors=True, NotifyOnValidationError=True。即验证数据错误,通知数据错误。
public class CommonDiseaseModel : ModelBase { private string symptomName; [Required(ErrorMessage = "常见病名称不能为空!")] public string SymptomName { get { return symptomName; } set { symptomName = value; RaisePropertyChanged("SymptomName"); } } private string symptomDetail; [Required(ErrorMessage = "症状不能为空!")] public string SymptomDetail { get { return symptomDetail; } set { symptomDetail = value; RaisePropertyChanged("SymptomDetail"); } } private string medication; [Required(ErrorMessage = "用药不能为空!")] public string Medication { get { return medication; } set { medication = value; RaisePropertyChanged("Medication"); } } private string treatmentPlan; [Required(ErrorMessage = "治疗方案不能为空!")] public string TreatmentPlan { get { return treatmentPlan; } set { treatmentPlan = value; RaisePropertyChanged("TreatmentPlan"); } } }
在Model中我们需要加入一些验证规则即可。当然了,我们还可以实现一些验证接口或者ValidateException。今天就到这里,老夫要睡觉了。
发表评论
匿名
用户评论
暂无评论