Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 两个日期之间的差异,并使用WPF将结果放入IntegerUpDown字段_C#_Wpf_Date - Fatal编程技术网

C# 两个日期之间的差异,并使用WPF将结果放入IntegerUpDown字段

C# 两个日期之间的差异,并使用WPF将结果放入IntegerUpDown字段,c#,wpf,date,C#,Wpf,Date,我正在使用WPF开发一个应用程序。 在我的一个窗口中,我有两个日期选择器和一个IntegerUpDown字段。 这就是我所拥有的-> 我想根据日期选择器中的日期更改IntegerUpDown字段中的值。例如,如果第一个日期为2017年9月2日,第二个日期为2017年9月7日,则该值应为5。 我看到了一个例子,但当我尝试它时,没有结果。值仍然是1 我将感激任何帮助 编辑 这是我的代码: DateTime dtDateCheckIn=dtpCheckIn.SelectedDate.Value.Da

我正在使用WPF开发一个应用程序。 在我的一个窗口中,我有两个日期选择器和一个IntegerUpDown字段。 这就是我所拥有的->

我想根据日期选择器中的日期更改IntegerUpDown字段中的值。例如,如果第一个日期为2017年9月2日,第二个日期为2017年9月7日,则该值应为5。 我看到了一个例子,但当我尝试它时,没有结果。值仍然是1

我将感激任何帮助

编辑 这是我的代码: DateTime dtDateCheckIn=dtpCheckIn.SelectedDate.Value.Date; DateTime dtDateCheckOut=dtpCheckOut.SelectedDate.Value.Date; TimeSpan差=dtDateCheckOut.SubtractdtDateCheckIn

编辑2


代码有点不同,因为我正在尝试不同的方法来获得结果并将其放入IntegerUpDown字段。我想我错过了什么,但我不知道是什么/

下面是一个使用MVVM模式的快速示例。我手边没有上/下控件,所以我只使用了一个普通的文本框,它应该很容易更换。它还使用了免责声明:这是一个我为ViewModel基类维护的库,但只要它引发属性更改通知,就可以将其替换为任何其他虚拟实现

每个日期选择器都绑定到视图模型上的DateTime字段。调用setter时,绑定到TextBox的Difference属性将更新为两个日期之间的差异

MainWindow.xaml:

<Window
    x:Class="WpfApp2.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:local="clr-namespace:WpfApp2"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    mc:Ignorable="d">
    <Window.DataContext>
        <local:MainWindowVm></local:MainWindowVm>
    </Window.DataContext>

    <StackPanel Orientation="Vertical">
        <DatePicker SelectedDate="{Binding Path=Date1}" />
        <DatePicker SelectedDate="{Binding Path=Date2}" />
        <TextBox Text="{Binding Path=Difference}" />
    </StackPanel>
</Window>

向我们展示代码…以及如何分配差异?您可能应该获取difference.Days并将其分配给IntegerUpDown控件。我尝试了difference.TotalDays.ToString,但没有结果。我将尝试你的建议。你将无法将字符串分配给操纵int的控件。获取天数并将其分配给控件的值。是的,我知道。一开始我试过使用.Text。这就是我使用的原因。ToString。谢谢你,@Bradley,我会检查一下。如果我有问题,我可以问你吗?当然可以!我很乐意帮忙。嘿,布拉德利。你能给我发一封电子邮件到gmail.com的slav4eto95吗?关于你的例子,我有一些问题。
<Window
    x:Class="WpfApp2.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:local="clr-namespace:WpfApp2"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    mc:Ignorable="d">
    <Window.DataContext>
        <local:MainWindowVm></local:MainWindowVm>
    </Window.DataContext>

    <StackPanel Orientation="Vertical">
        <DatePicker SelectedDate="{Binding Path=Date1}" />
        <DatePicker SelectedDate="{Binding Path=Date2}" />
        <TextBox Text="{Binding Path=Difference}" />
    </StackPanel>
</Window>
using System;
using AgentOctal.WpfLib;

namespace WpfDates
{
    class MainWindowVm : ViewModel
    {
        public MainWindowVm()
        {
            Date1 = new DateTime(2017, 1, 1);
            Date2 = new DateTime(2016, 1, 1);
        }

        private DateTime _date1;
        public DateTime Date1
        {
            get { return _date1; }
            set
            {
                SetValue(ref _date1, value);
                UpdateDifference();
            }
        }

        private DateTime _date2;
        public DateTime Date2
        {
            get { return _date2; }
            set
            {
                SetValue(ref _date2, value);
                UpdateDifference();
            }
        }

        private int _difference;
        public int Difference
        {
            get { return _difference; }
            set
            {
                SetValue(ref _difference, value);
            }
        }

        private void UpdateDifference()
        {
            Difference = (int)Math.Floor((Date1 - Date2).TotalDays);
        }
    }
}