just some code...: Question is at bottom.
XAML:
<StackPanel Orientation="Horizontal">
<Button Content="Start" Command="{Binding FirstDateCommand}" />
<Button Content="Back" Command="{Binding PreviousDateCommand}" />
<DatePicker SelectedDate="{Binding SelectedDate}" DisplayDateStart="{Binding MinDate}" DisplayDateEnd="{Binding MaxDate}" />
<Button Content="Forward" Command="{Binding NextDateCommand}" />
<Button Content="End" Command="{Binding LastDateCommand}" />
</StackPanel>
ViewModel:
public class LessonPlannerViewModel : ViewModelBase
{
private ILessonPlannerRepository _lessonplannerRepo;
private ObservableCollection<LessonDay> _lessons;
private RelayCommand _firstDateCommand;
private RelayCommand _lastDateCommand;
private RelayCommand _nextDateCommand;
private RelayCommand _previousDateCommand;
public LessonPlannerViewModel()
{
_lessonplannerRepo = new LessonPlannerRepository();
MinDate = DateTime.Now.AddDays(-2);
MaxDate = DateTime.Now.AddDays(2);
SelectedDate = DateTime.Now;
}
public RelayCommand FirstDateCommand
{
get { return _firstDateCommand ?? (_firstDateCommand = new RelayCommand(() => MoveFirstDate(), () => CanMoveFirstDate())); }
}
public RelayCommand LastDateCommand
{
get { return _lastDateCommand ?? (_lastDateCommand = new RelayCommand(() => MoveLastDate(), () => CanMoveLastDate())); }
}
public RelayCommand PreviousDateCommand
{
get { return _previousDateCommand ?? (_previousDateCommand = new RelayCommand(() => MovePreviousDate(), () => CanMovePreviousDate())); }
}
public RelayCommand NextDateCommand
{
get { return _nextDateCommand ?? (_nextDateCommand = new RelayCommand(() => MoveNextDate(), () => CanMoveNextDate())); }
}
private void MoveFirstDate()
{
SelectedDate = MinDate;
Lessons = _lessonplannerRepo.GetLessonDayByDate(SelectedDate);
}
private void MoveLastDate()
{
SelectedDate = MaxDate;
Lessons = _lessonplannerRepo.GetLessonDayByDate(SelectedDate);
}
private void MoveNextDate()
{
SelectedDate = SelectedDate.AddDays(1);
Lessons = _lessonplannerRepo.GetLessonDayByDate(SelectedDate);
}
private void MovePreviousDate()
{
SelectedDate = SelectedDate.AddDays(-1);
Lessons = _lessonplannerRepo.GetLessonDayByDate(SelectedDate);
}
private bool CanMoveFirstDate()
{
return SelectedDate != MinDate;
}
private bool CanMoveLastDate()
{
return SelectedDate != MaxDate;
}
private bool CanMoveNextDate()
{
return SelectedDate < MaxDate;
}
private bool CanMovePreviousDate()
{
return SelectedDate > MinDate;
}
private DateTime _selectedDate;
public DateTime SelectedDate
{
get { return _selectedDate; }
set
{
if (_selectedDate == value)
return;
_selectedDate = value;
this.RaisePropertyChanged("SelectedDate");
//Lessons = _lessonplannerRepo.GetLessonDayByDate(SelectedDate);
}
}
public DateTime MinDat开发者_开发知识库e { get; set; }
public DateTime MaxDate { get; set; }
public ObservableCollection<LessonDay> Lessons
{
get { return _lessons; }
set
{
_lessons = value;
this.RaisePropertyChanged("Lessons");
}
}
...
When I choose in the DatePicker a date which is equal to MinDate then the PreviousDateCommand returns CanExecute = false; thats ok and works as expected.
But why is the LastDateCommand not returning CanExecute = false too?
My CanExecute logic works as expected, when I press the PreviousDateButton instead of selecting the date via datepicker.
What do I wrong?
UPDATE:
I have not had any doubts that my logic is wrong but... I tried some things and with this code
this is really weird. I changed now the logic of the LastDate and PreviousDate CanExecute method and both buttons work now changing the datepicker.
private bool CanMoveFirstDate()
{
Debug.WriteLine("SelectedDate FirstDate: " + SelectedDate);
return SelectedDate > MinDate;
}
private bool CanMovePreviousDate()
{
Debug.WriteLine("SelectedDate PreviousDate: " + SelectedDate);
return SelectedDate > MinDate;
}
Is someone knows how to make the NextDate + LastDate button working gets the solution! :P
UPDATE 2:
Bindings are powerfull but maybe hard to control...
I did some crazy logic shit again and now it seems to work:
private bool CanMoveNextDate()
{
Debug.WriteLine("SelectedDate NextDate: " + SelectedDate);
return SelectedDate.AddDays(1) < MaxDate;
}
private bool CanMoveLastDate()
{
Debug.WriteLine("SelectedDate LastDate: " + SelectedDate);
return SelectedDate.AddDays(1) < MaxDate;
}
If someone can explain that weird logic, that would be nice , I think the cause lays in the binding of the datepicker and the commands and which binding gets updated first or is called etc...
tststs... this is really a lesson to me:
Instead of putting this is the viewmodel constructor:
MinDate = DateTime.Now.AddDays(-2);
MaxDate = DateTime.Now.AddDays(2);
put this:
MinDate = DateTime.Parse("28.07.2010 00:00:00");
MaxDate = DateTime.Parse("01.08.2010 00:00:00");
because SelectedDate is always formatted like this:
dd.MM.yyyy 00:00:00
I want to say Microsoft thank you for their great debugging tools in VS 2010 =>
http://img833.imageshack.us/img833/5912/cryforariver.png
and I already cursed the wpf binding system :P a god damn user error now go and slap me I
deserve it! but the points are mine :P
精彩评论