首頁編輯與完成
- indexview添加Listbox控件的鼠標(biāo)雙擊行為
- 添加todo、memo的編輯命令
- indexviewmodel添加對應(yīng)的更新事件處理
- 添加ToggleButton與后臺的綁定
- 將ToggleButton的ischeck綁定到status屬性
- 添加bool int 轉(zhuǎn)換器
- 添加完成命令
- 添加完成功能函數(shù)
Listbox添加行為
給行為添加命令空間
文件:Mytodo.Views.IndexView.cs
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
添加行為事件
文件:Mytodo.Views.IndexView.cs
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding EditTodoCmd}" CommandParameter="{Binding ElementName=todolbox, Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding EditTodoCmd}" CommandParameter="{Binding ElementName=todolbox, Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
后臺添加對應(yīng)的命令,并初始化
文件:Mytodo.Views.IndexViewmodel.cs
/// <summary>
/// 命令:編輯備忘
/// </summary>
public DelegateCommand<MemoDto> EditMemoCmd { get;private set; }
/// <summary>
/// 命令:編輯待辦
/// </summary>
public DelegateCommand<ToDoDto> EditTodoCmd { get; private set; }
//初始化命令
EditMemoCmd = new DelegateCommand<MemoDto>(Addmemo);
EditTodoCmd = new DelegateCommand<ToDoDto>(Addtodo);
添加命令方法
文件:Mytodo.Views.IndexViewmodel.cs
/// <summary>
/// 添加待辦事項
/// </summary>
async void Addtodo(ToDoDto model)
{
DialogParameters param = new DialogParameters();
if (model != null)
param.Add("Value", model);
var dialogres = await dialog.ShowDialog("AddTodoView", param);
var newtodo = dialogres.Parameters.GetValue<ToDoDto>("Value");
if (newtodo == null || string.IsNullOrEmpty(newtodo.Title) || (string.IsNullOrEmpty(newtodo.Content)))
return;
if (dialogres.Result == ButtonResult.OK)
{
try
{
if (newtodo.Id > 0)
{
var updres = await toDoService.UpdateAsync(newtodo);
if (updres.Status)
{
var todo = TodoDtos.FirstOrDefault(x=>x.Id.Equals(newtodo.Id));
//更新信息
todo.Content = newtodo.Content;
todo.Title = newtodo.Title;
todo.Status = newtodo.Status;
}
}
else
{
//添加內(nèi)容
//更新數(shù)據(jù)庫數(shù)據(jù)
var addres = await toDoService.AddAsync(newtodo);
//更新UI數(shù)據(jù)
if (addres.Status)
{
TodoDtos.Add(addres.Result);
}
}
}
catch
{
}
finally
{
UpdateLoding(false);
}
}
}
/// <summary>
/// 添加備忘錄
/// </summary>
async void Addmemo(MemoDto model)
{
DialogParameters param = new DialogParameters();
if (model != null)
param.Add("Value", model);
var dialogres = await dialog.ShowDialog("AddMemoView", param);
if (dialogres.Result == ButtonResult.OK)
{
try
{
var newmemo = dialogres.Parameters.GetValue<MemoDto>("Value");
if (newmemo != null && string.IsNullOrWhiteSpace(newmemo.Content) && string.IsNullOrWhiteSpace(newmemo.Title))
return;
if (newmemo.Id > 0)
{
var updres = await memoService.UpdateAsync(newmemo);
if (updres.Status)
{
//var memo = MemoDtos.FindFirst(predicate: x => x.Id == newmemo.Id);
var memo = MemoDtos.FirstOrDefault( x => x.Id.Equals( newmemo.Id));
//更新信息
memo.Content = newmemo.Content;
memo.Title = newmemo.Title;
}
}
else
{
//添加內(nèi)容
var addres = await memoService.AddAsync(newmemo);
//更新UI數(shù)據(jù)
if (addres.Status)
{
MemoDtos.Add(addres.Result);
}
}
}
catch
{
}
finally
{
UpdateLoding(false);
}
}
}
添加ToggleButton綁定與check事件
添加轉(zhuǎn)換器
添加文件:Mytodo.Common.Converters.BoolInt_TConverter.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace Mytodo.Common.Converters
{
public class BoolInt_TConverter : IValueConverter
{
/// <summary>
/// int to bool
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value!=null&&int.TryParse(value.ToString(),out int result))
{
if(result==0)
return true;
else
return false;
}
return false;
}
/// <summary>
/// bool to int
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && bool.TryParse(value.ToString(), out bool result))
{
if (result)
return 0;
else
return 1;
}
return false;
}
}
}
添加轉(zhuǎn)換器,添加命令
修改文件:Mytodo.Views.IndexView.xaml
- 添加命名空間
xmlns:cv="clr-namespace:Mytodo.Common.Converters"
-
添加資源
<UserControl.Resources> <ResourceDictionary> <cv:BoolInt_TConverter x:Key="BoolInt_TConverter" /> </ResourceDictionary> </UserControl.Resources>
-
綁定命令,添加轉(zhuǎn)換器
<ToggleButton Width="40" Command="{Binding DataContext.ToDoCompltedCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}}" CommandParameter="{Binding }" DockPanel.Dock="Right" IsChecked="{Binding Status, Converter={StaticResource BoolInt_TConverter}, Mode=TwoWay}" /> <StackPanel>
-
定義命令,并初始化文章來源:http://www.zghlxwxcb.cn/news/detail-620243.html
/// <summary> /// Todo完成命令 /// </summary> public DelegateCommand<ToDoDto> ToDoCompltedCommand { get; set; } public IndexViewModel(IContainerProvider provider, IDialogHostService dialog) : base(provider) { //實例化接口 this.toDoService= provider.Resolve<ITodoService>(); this.memoService = provider.Resolve<IMemoService>(); //實例化對象 MemoDtos = new ObservableCollection<MemoDto>(); TodoDtos = new ObservableCollection<ToDoDto>(); //初始化命令 EditMemoCmd = new DelegateCommand<MemoDto>(Addmemo); EditTodoCmd = new DelegateCommand<ToDoDto>(Addtodo); ToDoCompltedCommand = new DelegateCommand<ToDoDto>(Compete); ExecuteCommand = new DelegateCommand<string>(Execute); this.dialog = dialog; CreatBars(); }
-
初始化命令操作函數(shù)文章來源地址http://www.zghlxwxcb.cn/news/detail-620243.html
/// <summary> /// togglebutoon 的命令 /// </summary> /// <param name="dto"></param> /// <exception cref="NotImplementedException"></exception> async private void Compete(ToDoDto dto) { if (dto == null || string.IsNullOrEmpty(dto.Title) || (string.IsNullOrEmpty(dto.Content))) return; var updres = await toDoService.UpdateAsync(dto); if (updres.Status) { var todo = TodoDtos.FirstOrDefault(x => x.Id.Equals(dto.Id)); //更新信息 todo.Status = dto.Status; } }
修改相關(guān)bug
修改Mytodo.ViewModels.cs
using Mytodo.Common.Models;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using MyToDo.Share.Models;
using Prism.Commands;
using Prism.Services.Dialogs;
using Mytodo.Dialog;
using Mytodo.ViewModels;
using Mytodo.Service;
using Prism.Ioc;
using System.Diagnostics;
using Microsoft.VisualBasic;
using ImTools;
using DryIoc;
using MyToDo.Share;
using System.Windows;
namespace Mytodo.ViewModels
{
public class IndexViewModel:NavigationViewModel
{
#region 定義命令
/// <summary>
/// Todo完成命令
/// </summary>
public DelegateCommand<ToDoDto> ToDoCompltedCommand { get; set; }
public DelegateCommand<string> ExecuteCommand { get; set; }
/// <summary>
/// 命令:編輯備忘
/// </summary>
public DelegateCommand<MemoDto> EditMemoCmd { get;private set; }
/// <summary>
/// 命令:編輯待辦
/// </summary>
public DelegateCommand<ToDoDto> EditTodoCmd { get; private set; }
#endregion
#region 定義屬性
public string Title { get; set; }
public ObservableCollection<MemoDto> MemoDtos
{
get { return memoDtos; }
set { memoDtos = value; RaisePropertyChanged(); }
}
public ObservableCollection<ToDoDto> TodoDtos
{
get { return todoDtos; }
set { todoDtos = value; RaisePropertyChanged(); }
}
/// <summary>
/// 首頁任務(wù)條
/// </summary>
public ObservableCollection<TaskBar> TaskBars
{
get { return taskBars; }
set { taskBars = value; RaisePropertyChanged(); }
}
#endregion
#region 定義重要命令
#endregion
#region 定義重要字段
private readonly IDialogHostService dialog;
private readonly ITodoService toDoService;
private readonly IMemoService memoService;
#endregion
#region 定義普通字段
private ObservableCollection<TaskBar> taskBars;
private ObservableCollection<ToDoDto> todoDtos;
private ObservableCollection<MemoDto> memoDtos;
#endregion
#region 命令相關(guān)方法
/// <summary>
/// togglebutoon 的命令
/// </summary>
/// <param name="dto"></param>
/// <exception cref="NotImplementedException"></exception>
async private void Compete(ToDoDto dto)
{
if (dto == null || string.IsNullOrEmpty(dto.Title) || (string.IsNullOrEmpty(dto.Content)))
return;
var updres = await toDoService.UpdateAsync(dto);
if (updres.Status)
{
var todo = TodoDtos.FirstOrDefault(x => x.Id.Equals(dto.Id));
//更新信息
todo.Status = dto.Status;
}
}
/// <summary>
/// 選擇執(zhí)行命令
/// </summary>
/// <param name="obj"></param>
void Execute(string obj)
{
switch (obj)
{
case "新增待辦": Addtodo(null); break;
case "新增備忘": Addmemo(null); break;
}
}
/// <summary>
/// 添加待辦事項
/// </summary>
async void Addtodo(ToDoDto model)
{
DialogParameters param = new DialogParameters();
if (model != null)
param.Add("Value", model);
var dialogres = await dialog.ShowDialog("AddTodoView", param);
var newtodo = dialogres.Parameters.GetValue<ToDoDto>("Value");
if (newtodo == null || string.IsNullOrEmpty(newtodo.Title) || (string.IsNullOrEmpty(newtodo.Content)))
return;
if (dialogres.Result == ButtonResult.OK)
{
try
{
if (newtodo.Id > 0)
{
var updres = await toDoService.UpdateAsync(newtodo);
if (updres.Status)
{
var todo = TodoDtos.FirstOrDefault(x=>x.Id.Equals(newtodo.Id));
//更新信息
todo.Content = newtodo.Content;
todo.Title = newtodo.Title;
todo.Status = newtodo.Status;
}
}
else
{
//添加內(nèi)容
//更新數(shù)據(jù)庫數(shù)據(jù)
var addres = await toDoService.AddAsync(newtodo);
//更新UI數(shù)據(jù)
if (addres.Status)
{
TodoDtos.Add(addres.Result);
}
}
}
catch
{
}
finally
{
UpdateLoding(false);
}
}
}
/// <summary>
/// 添加備忘錄
/// </summary>
async void Addmemo(MemoDto model)
{
DialogParameters param = new DialogParameters();
if (model != null)
param.Add("Value", model);
var dialogres = await dialog.ShowDialog("AddMemoView", param);
if (dialogres.Result == ButtonResult.OK)
{
try
{
var newmemo = dialogres.Parameters.GetValue<MemoDto>("Value");
if (newmemo != null && string.IsNullOrWhiteSpace(newmemo.Content) && string.IsNullOrWhiteSpace(newmemo.Title))
return;
if (newmemo.Id > 0)
{
var updres = await memoService.UpdateAsync(newmemo);
if (updres.Status)
{
//var memo = MemoDtos.FindFirst(predicate: x => x.Id == newmemo.Id);
var memo = MemoDtos.FirstOrDefault( x => x.Id.Equals( newmemo.Id));
//更新信息
memo.Content = newmemo.Content;
memo.Title = newmemo.Title;
}
}
else
{
//添加內(nèi)容
var addres = await memoService.AddAsync(newmemo);
//更新UI數(shù)據(jù)
if (addres.Status)
{
MemoDtos.Add(addres.Result);
}
}
}
catch
{
}
finally
{
UpdateLoding(false);
}
}
}
#endregion
#region 其它方法
#endregion
#region 啟動項相關(guān)
void CreatBars()
{
Title = "您好,2022";
TaskBars = new ObservableCollection<TaskBar>();
TaskBars.Add(new TaskBar { Icon = "CalendarBlankOutline", Title = "匯總", Color = "#FF00FF00", Content = "27", Target = "" });
TaskBars.Add(new TaskBar { Icon = "CalendarMultipleCheck", Title = "已完成", Color = "#6B238E", Content = "24", Target = "" });
TaskBars.Add(new TaskBar { Icon = "ChartLine", Title = "完成比例", Color = "#32CD99", Content = "100%", Target = "" });
TaskBars.Add(new TaskBar { Icon = "CheckboxMarked", Title = "備忘錄", Color = "#5959AB", Content = "13", Target = "" });
}
#endregion
public IndexViewModel(IContainerProvider provider,
IDialogHostService dialog) : base(provider)
{
//實例化接口
this.toDoService= provider.Resolve<ITodoService>();
this.memoService = provider.Resolve<IMemoService>();
//實例化對象
MemoDtos = new ObservableCollection<MemoDto>();
TodoDtos = new ObservableCollection<ToDoDto>();
//初始化命令
EditMemoCmd = new DelegateCommand<MemoDto>(Addmemo);
EditTodoCmd = new DelegateCommand<ToDoDto>(Addtodo);
ToDoCompltedCommand = new DelegateCommand<ToDoDto>(Compete);
ExecuteCommand = new DelegateCommand<string>(Execute);
this.dialog = dialog;
CreatBars();
}
}
}
修改Mytodo.AddMemoViewModel.cs
using MaterialDesignThemes.Wpf;
using Mytodo.Dialog;
using MyToDo.Share.Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mytodo.ViewModels.Dialogs
{
internal class AddMemoViewModel : BindableBase, IDialogHostAware
{
public AddMemoViewModel()
{
SaveCommand = new DelegateCommand(Save);
CancelCommand = new DelegateCommand(Cancel);
}
private MemoDto model;
public MemoDto Model
{
get { return model; }
set { model = value; RaisePropertyChanged(); }
}
private void Cancel()
{
if (DialogHost.IsDialogOpen(DialogHostName))
DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
}
private void Save()
{
if (DialogHost.IsDialogOpen(DialogHostName))
{
//確定時,把編輯的實體返回并且返回OK
DialogParameters param = new DialogParameters();
param.Add("Value", Model);
DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.OK, param));
}
}
public string DialogHostName { get; set; }
public DelegateCommand SaveCommand { get; set; }
public DelegateCommand CancelCommand { get; set; }
public void OnDialogOpend(IDialogParameters parameters)
{
if (parameters.ContainsKey("Value"))
{
Model = parameters.GetValue<MemoDto>("Value");
if(Model == null) {
Model = new MemoDto();
}c
}
else
Model = new MemoDto();
}
}
}
修改Mytodo.AddTodoViewModel.cs
using MaterialDesignThemes.Wpf;
using Mytodo.Dialog;
using MyToDo.Share.Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mytodo.ViewModels.Dialogs
{
internal class AddTodoViewModel : BindableBase, IDialogHostAware
{
public AddTodoViewModel()
{
SaveCommand = new DelegateCommand(Save);
CancelCommand = new DelegateCommand(Cancel);
}
private ToDoDto model;
/// <summary>
/// 新增或編輯的實體
/// </summary>
public ToDoDto Model
{
get { return model; }
set { model = value; RaisePropertyChanged(); }
}
/// <summary>
/// 取消
/// </summary>
private void Cancel()
{
if (DialogHost.IsDialogOpen(DialogHostName))
DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No)); //取消返回NO告訴操作結(jié)束
}
/// <summary>
/// 確定
/// </summary>
private void Save()
{
if (DialogHost.IsDialogOpen(DialogHostName))
{
//確定時,把編輯的實體返回并且返回OK
DialogParameters param = new DialogParameters();
param.Add("Value", Model);
DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.OK, param));
}
}
public string DialogHostName { get; set; }
public DelegateCommand SaveCommand { get; set; }
public DelegateCommand CancelCommand { get; set; }
public void OnDialogOpend(IDialogParameters parameters)
{
if (parameters.ContainsKey("Value"))
{
Model = parameters.GetValue<ToDoDto>("Value");
if (Model == null)
{
Model = new ToDoDto();
Model.Status = 1;
}
}
else
{
Model = new ToDoDto();
Model.Status = 1;
}
}
}
}
到了這里,關(guān)于WPF實戰(zhàn)學(xué)習(xí)筆記24-首頁編輯與完成的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!