只是一些代碼... wpf按鈕的奇怪行爲:問題在底部。WPF/MVVM:使用命令禁用
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>
視圖模型:
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 MinDate { get; set; }
public DateTime MaxDate { get; set; }
public ObservableCollection<LessonDay> Lessons
{
get { return _lessons; }
set
{
_lessons = value;
this.RaisePropertyChanged("Lessons");
}
}
...
當我在的DatePicker選擇日期等於爲MinDate則PreviousDateCommand返回CanExecute = FALSE; t 帽子確定並按預期工作。
但是爲什麼LastDateCommand 不能返回CanExecute = false呢?
我CanExecute邏輯正常工作,當我按下PreviousDateButton代替通過日期選擇器選擇的日期。
我該怎麼做?
UPDATE:
我沒有任何懷疑,我的邏輯是錯誤的,但......我嘗試了一些東西,這個代碼
這實在是不可思議。我現在改變了LastDate和PreviousDate CanExecute方法的邏輯,現在兩個按鈕都可以改變日期選擇器。
private bool CanMoveFirstDate()
{
Debug.WriteLine("SelectedDate FirstDate: " + SelectedDate);
return SelectedDate > MinDate;
}
private bool CanMovePreviousDate()
{
Debug.WriteLine("SelectedDate PreviousDate: " + SelectedDate);
return SelectedDate > MinDate;
}
有人知道如何使NextDate + LastDate按鈕工作得到解決方案! :P
更新2:
綁定是強大的,但也許很難控制......
我又做了一些瘋狂的狗屎邏輯,現在它似乎工作:
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;
}
如果有人可以解釋古怪的邏輯,這將是很好的,我認爲原因奠定了日期選擇器和命令的綁定,並且哪個綁定被更新第一次或被稱爲等...
我不知道我明白這個問題,你似乎保存,當選定的日期== MinDate,PreviousDateCommand.CanExecute()返回false和LastDateCommand.CanExecute()返回true,這聽起來正確。有什麼問題,這是不同的行爲取決於是否使用DatePicker選擇日期與按鈕? – 2010-07-27 21:33:36
「...它是不同的行爲取決於是否使用DatePicker選擇日期與按鈕?」 => 是的,除了PreviousDateCommand之外,它可以選擇日期選擇器。 – msfanboy 2010-07-28 05:39:41
@msfanboy:如果LastDateCommand正在創建問題,那麼您應該使用LastDateButton而不是PreviousDateButton來反檢查結果,而這已經正常工作了。儘管這個問題似乎很奇怪。調試器說什麼? – Amsakanna 2010-07-28 19:15:25