TodoView 添加新增、编辑、查询功能
修改TodoViewModel.cs
using Mytodo.Common.Models;
using Mytodo.Service;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using MyToDo.Share.Models;
using System.Threading.Tasks;
using Prism.Regions;
using System.Windows;
namespace Mytodo.ViewModels
{
public class TodoViewModel: NavigationViewModel
{
#region 命令定义
/// <summary>
/// 展开侧边栏
/// </summary>
public DelegateCommand OpenRightContentCmd { set; get; }
/// <summary>
/// 打开选择的项
/// </summary>
public DelegateCommand<ToDoDto> SelectedCommand { get; set; }
/// <summary>
/// 添加、编辑 项
/// </summary>
public DelegateCommand<string> ExecuteCommand { get; set; }
#endregion
#region 属性定义
/// <summary>
/// 当前选中项
/// </summary>
public ToDoDto? CurrDto
{
get { return currDto; }
set { currDto = value; RaisePropertyChanged(); }
}
/// <summary>
/// 指示侧边栏是否展开
/// </summary>
public bool IsRightOpen
{
get { return isRightOpen; }
set { isRightOpen = value; RaisePropertyChanged(); }
}
/// <summary>
/// todo集合
/// </summary>
public ObservableCollection<ToDoDto>? TodoDtos
{
get { return todoDtos; }
set { todoDtos = value; RaisePropertyChanged(); }
}
/// <summary>
/// 右侧侧边栏标题
/// </summary>
public string RightContentTitle
{
get { return rightContentTitle; }
set { rightContentTitle = value;RaisePropertyChanged(); }
}
/// <summary>
/// 要搜索的字符串
/// </summary>
public string SearchString
{
get { return search; }
set { search = value; RaisePropertyChanged(); }
}
#endregion
#region 重要字段定义
private readonly ITodoService service;
#endregion
#region 字段定义
private ToDoDto currDto;
private bool isRightOpen;
private ObservableCollection<ToDoDto>? todoDtos;
private string rightContentTitle;
private string search;
#endregion
#region 命令方法
private void ExceuteCmd(string obj)
{
switch (obj)
{
case "添加":
Add(); break;
case "查询":
Query();break;
case "保存":
Save(); break;
}
}
private async void Save()
{
try
{
if (string.IsNullOrWhiteSpace(CurrDto.Title) || string.IsNullOrWhiteSpace(CurrDto.Content))
return;
UpdateLoding(true);
if(CurrDto.Id>0) //编辑项
{
var updateres = await service.UpdateAsync(CurrDto);
if (updateres.Status)
{
var todo = TodoDtos.FirstOrDefault(t => t.Id == CurrDto.Id);
if (todo != null)
{
todo.Title=CurrDto.Title;
todo.Content=CurrDto.Content;
todo.Status=todo.Status;
}
}
else
{
MessageBox.Show("更新失败");
}
}
else
{
//添加项
var add_res = await service.AddAsync(CurrDto);
//刷新
if (add_res.Status) //如果添加成功
{
TodoDtos.Add(add_res.Result);
}
else
{
MessageBox.Show("添加失败");
}
}
}
catch
{
}
finally
{
IsRightOpen = false;
//卸载数据加载窗体
UpdateLoding(false);
}
}
/// <summary>
/// 打开待办事项弹窗
/// </summary>
void Add()
{
CurrDto = new ToDoDto();
IsRightOpen = true;
}
private void Query()
{
GetDataAsync();
}
/// <summary>
/// 获取所有数据
/// </summary>
async void GetDataAsync()
{
//调用数据加载页面
UpdateLoding(true);
var todoResult = await service.GetAllAsync(new MyToDo.Share.Parameters.QueryParameter { PageIndex = 0, PageSize = 100,Search=SearchString });
if (todoResult.Status)
{
todoDtos.Clear();
foreach (var item in todoResult.Result.Items)
todoDtos.Add(item);
}
//卸载数据加载页面
UpdateLoding(false);
}
/// <summary>
/// 弹出详细信息
/// </summary>
/// <param name="obj"></param>
private async void Selected(ToDoDto obj)
{
var todores = await service.GetFirstOfDefaultAsync(obj.Id);
if(todores.Status)
{
CurrDto = todores.Result;
IsRightOpen = true;
RightContentTitle = "我的待办";
}
}
#endregion
public TodoViewModel(ITodoService service,IContainerProvider provider) : base(provider)
{
//初始化对象
TodoDtos = new ObservableCollection<ToDoDto>();
RightContentTitle = "添加血雨待办";
//初始化命令
SelectedCommand = new DelegateCommand<ToDoDto>(Selected);
OpenRightContentCmd = new DelegateCommand(Add);
ExecuteCommand = new DelegateCommand<string>(ExceuteCmd);
this.service = service;
}
public override void OnNavigatedTo(NavigationContext navigationContext)
{
base.OnNavigatedTo(navigationContext);
GetDataAsync();
}
}
}
修改XAML
添加引用
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
添加绑定
<md:DrawerHost.RightDrawerContent>
<DockPanel
MinWidth="200"
MaxWidth="240"
Margin="2"
LastChildFill="False">
<TextBlock
Margin="10"
DockPanel.Dock="Top"
FontFamily="微软雅黑"
FontSize="20"
FontWeight="Bold"
Text="{Binding RightContentTitle}" />
<StackPanel
Margin="10"
DockPanel.Dock="Top"
Orientation="Horizontal">
<TextBlock
Margin="5"
VerticalAlignment="Center"
FontFamily="微软雅黑"
FontSize="14"
Text="状态" />
<ComboBox Margin="5">
<ComboBoxItem Content="已完成" FontSize="12" />
<ComboBoxItem Content="未完成" FontSize="12" />
</ComboBox>
</StackPanel>
<TextBox
Margin="10"
md:HintAssist.Hint="待办事项标题"
DockPanel.Dock="Top"
FontFamily="微软雅黑"
FontSize="12"
Text="{Binding CurrDto.Title}" />
<TextBox
MinHeight="50"
Margin="10"
md:HintAssist.Hint="待办事项内容"
DockPanel.Dock="Top"
FontFamily="微软雅黑"
FontSize="12"
Text="{Binding CurrDto.Content}"
TextWrapping="Wrap" />
<Button
Margin="10,5"
HorizontalAlignment="Center"
Command="{Binding ExecuteCommand}"
CommandParameter="保存"
Content="保存"
DockPanel.Dock="Top" />
</DockPanel>
</md:DrawerHost.RightDrawerContent>
添加项目的双击事件文章来源:https://www.toymoban.com/news/detail-614657.html
<Border MinWidth="200" Margin="10">
<Grid MinHeight="150">
<!-- 给项目添加行为 -->
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding DataContext.SelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}" CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition />
</Grid.RowDefinitions>
<DockPanel LastChildFill="False">
<TextBlock
Margin="10,10"
FontFamily="黑体"
FontSize="14"
Text="{Binding Title}" />
<md:PackIcon
Margin="10,10"
VerticalContentAlignment="Top"
DockPanel.Dock="Right"
Kind="More" />
</DockPanel>
<TextBlock
Grid.Row="1"
Margin="10,5"
FontFamily="黑体"
FontSize="12"
Opacity="0.7"
Text="{Binding Content}" />
<Canvas Grid.RowSpan="2" ClipToBounds="True">
<Border
Canvas.Top="10"
Canvas.Right="-50"
Width="120"
Height="120"
Background="#FFFFFF"
CornerRadius="100"
Opacity="0.1" />
<Border
Canvas.Top="80"
Canvas.Right="-30"
Width="120"
Height="120"
Background="#FFFFFF"
CornerRadius="100"
Opacity="0.1" />
</Canvas>
<Border
Grid.RowSpan="2"
Background="#ffcccc"
CornerRadius="5"
Opacity="0.3" />
</Grid>
</Border>
修改ToDoService
修改MyToDo.Api/Service/ToDoService.cs文章来源地址https://www.toymoban.com/news/detail-614657.html
public async Task<ApiReponse> GetAllAsync(QueryParameter parameter)
{
try
{
//获取数据
var resposity = work.GetRepository<Todo>();
//根据查询条件查询
var todos = await resposity.GetPagedListAsync(predicate: x => string.IsNullOrWhiteSpace(parameter.Search) ? true : x.Title.Contains(parameter.Search), pageIndex: parameter.PageIndex, pageSize: parameter.PageSize, orderBy: source => source.OrderByDescending(t => t.CreateDate));
return new ApiReponse(true, todos);
}
catch (Exception ex)
{
return new ApiReponse(ex.Message, false);
}
}
到了这里,关于WPF实战学习笔记17-TodoView 添加新增、编辑、查询功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!