按钮增加日期

时间:2014-06-04 15:23:38

标签: c# wpf

我有一个StartDate日期选择器和一个EndDate日期选择器。我手动能够更改日期和过滤器应用和工作。我只在3个月的间隔内工作。所以我有一个previousnext的按钮。我遇到的问题是当我按下按钮时界面不会改变。

xaml方:

<DatePicker x:Name="StartDate" SelectedDate="{Binding StartDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DatePicker x:Name="EndDate" SelectedDate="{Binding EndDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="Previous" Width="95" cal:Message.Attach="[Click] = [Previous]" ></Button>
<Button Content="Next" Width="95" cal:Message.Attach="[Click] = [Next]" ></Button>

MVVM方面:

private DateTime _endDate;
public DateTime EndDate{ get {return _endDate;} set {_endDate = value;Filter();this.OnPropertyChanged("EndDate");}}
private DateTime _startDate;
public DateTime StartDate { get { return _startDate; } set { _startDate = value; Filter(); this.OnPropertyChanged("StartDate"); } }

public void Next()
{
    StartDate.AddMonths(3);
    EndDate.AddMonths(3);
    this.OnPropertyChanged("StartDate");
    this.OnPropertyChanged("EndDate");
}

public void Previous()
{
    StartDate.AddMonths(-3);
    EndDate.AddMonths(-3);
    this.OnPropertyChanged("StartDate");
    this.OnPropertyChanged("EndDate");
}

1 个答案:

答案 0 :(得分:1)

您忘记了实际设置日期属性。调用AddMonths会返回一个新的DateTime对象,而不是添加到现有对象。

public void Next()
{
    this.StartDate = StartDate.AddMonths(3);
    this.EndDate = EndDate.AddMonths(3);
    this.OnPropertyChanged("StartDate");
    this.OnPropertyChanged("EndDate");
}

public void Previous()
{
    this.StartDate = StartDate.AddMonths(-3);
    this.EndDate = EndDate.AddMonths(-3);
    this.OnPropertyChanged("StartDate");
    this.OnPropertyChanged("EndDate");
}