使用WPF打算通过MVVM框架设计一个计算两个日期之间天数的项目
结果无论怎么运行结果都是0
求天数的方法是运用了TimeSpan的Substrict
UI如图
在跟踪过程中发现,结果文本框绑定的“Result"的值已经变化了
但是UI上的结果还是0
实在找不到原因在哪
XML代码:
<Window x:Class="TimeCaculate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TimeCaculate"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="150"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="300"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Calendar Name="StartTime" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" SelectedDate="{Binding DateTime1}" />
<Calendar Name="EndTime" Grid.Column="2" Grid.Row="0" SelectedDate="{Binding DateTime2}"/>
<Button Name="Check" Content="Check" Height="100" Width="80" Grid.Column="0" Grid.Row="1" Command="{Binding AddCommand}"/>
<TextBox Name="ShowDays" Height="100" Width="680" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" Text="{Binding Result}"/>
</Grid>
</Window>
自定义Command:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace TimeCaculate.Commands
{
class DelegateCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
if (this.CanExecuteFunc == null)
{
return true;
}
return this.CanExecute(parameter);
}
public void Execute(object parameter)
{
if (this.ExecuteAction == null)
{
return;
}
this.ExecuteAction(parameter);
}
public Action<object> ExecuteAction { get; set; }//声明无返回值的EXECUTE委托
public Func<object, bool> CanExecuteFunc { get; set; }//声明CanExecute委托
}
}
ViewModel代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Prism.Mvvm;
using TimeCaculate.Commands;
namespace TimeCaculate.Models
{
internal class MainWindowModel:INotifyCompletion
{
public event PropertyChangedEventHandler PropertyChanged;
private DateTime datetime1;
public DateTime DateTime1
{
get { return datetime1; }
set
{
datetime1 = value;
if(this.PropertyChanged!=null)
{
this.PropertyChanged.Invoke(this,new PropertyChangedEventArgs("DataTime1"));
}
}
}
private DateTime datetime2;
public DateTime DateTime2
{
get { return datetime2; }
set
{
datetime2 = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("DataTime2"));
}
}
}
private int result;
public int Result
{
get { return result; }
set
{
result = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Result"));
}
}
}
//面向check按钮的命令
public DelegateCommand AddCommand { get; set; }
//创建计算天数的方法
private void Add(object parameter)
{
this.Result= this.DateTime2.Subtract(this.DateTime1).Days;
}
public MainWindowModel()//创建面向面板的命令实例
{
this.AddCommand = new DelegateCommand();//AddCommand引用自定义命令
//将Add方法赋予AddCommand的委托,
this.AddCommand.ExecuteAction = new Action<object>(this.Add);
}
public void OnCompleted(Action continuation)
{
throw new NotImplementedException();
}
}
}