今天也做好我自己 2023-02-28 23:53 采纳率: 100%
浏览 21
已结题

WPF使用MVVM框架实现一个计算天数的项目,结果绑定源的值变化了,绑定对象的值还没变。

使用WPF打算通过MVVM框架设计一个计算两个日期之间天数的项目
结果无论怎么运行结果都是0
求天数的方法是运用了TimeSpan的Substrict
UI如图

img

在跟踪过程中发现,结果文本框绑定的“Result"的值已经变化了

img

但是UI上的结果还是0

img


实在找不到原因在哪
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();
        }

    }
}

  • 写回答

2条回答 默认 最新

  • 今天也做好我自己 2023-03-01 09:55
    关注

    好吧,破案了,改用BindableBase的RaisePropertyChanged解决了,还是绑定设置问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月9日
  • 已采纳回答 3月1日
  • 创建了问题 2月28日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改