Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF/MVVM: Weird behaviour of wpf buttons with Commands disabling
    primarykey
    data
    text
    <p>just some code...: <strong>Question is at bottom.</strong></p> <p><strong>XAML</strong>:</p> <pre><code> &lt;StackPanel Orientation="Horizontal"&gt; &lt;Button Content="Start" Command="{Binding FirstDateCommand}" /&gt; &lt;Button Content="Back" Command="{Binding PreviousDateCommand}" /&gt; &lt;DatePicker SelectedDate="{Binding SelectedDate}" DisplayDateStart="{Binding MinDate}" DisplayDateEnd="{Binding MaxDate}" /&gt; &lt;Button Content="Forward" Command="{Binding NextDateCommand}" /&gt; &lt;Button Content="End" Command="{Binding LastDateCommand}" /&gt; &lt;/StackPanel&gt; </code></pre> <p><strong>ViewModel</strong>:</p> <pre><code>public class LessonPlannerViewModel : ViewModelBase { private ILessonPlannerRepository _lessonplannerRepo; private ObservableCollection&lt;LessonDay&gt; _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(() =&gt; MoveFirstDate(), () =&gt; CanMoveFirstDate())); } } public RelayCommand LastDateCommand { get { return _lastDateCommand ?? (_lastDateCommand = new RelayCommand(() =&gt; MoveLastDate(), () =&gt; CanMoveLastDate())); } } public RelayCommand PreviousDateCommand { get { return _previousDateCommand ?? (_previousDateCommand = new RelayCommand(() =&gt; MovePreviousDate(), () =&gt; CanMovePreviousDate())); } } public RelayCommand NextDateCommand { get { return _nextDateCommand ?? (_nextDateCommand = new RelayCommand(() =&gt; MoveNextDate(), () =&gt; 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 &lt; MaxDate; } private bool CanMovePreviousDate() { return SelectedDate &gt; 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&lt;LessonDay&gt; Lessons { get { return _lessons; } set { _lessons = value; this.RaisePropertyChanged("Lessons"); } } ... </code></pre> <p>When I choose in the DatePicker a date which is equal to MinDate then the PreviousDateCommand returns CanExecute = false; t<strong>hats ok and works as expected</strong>.</p> <p>But why is the LastDateCommand <strong>not returning CanExecute = false too?</strong> </p> <p>My CanExecute logic works as expected, when I press the <strong>PreviousDateButton</strong> <em>instead</em> of selecting the date via <strong>datepicker</strong>.</p> <p>What do I wrong?</p> <p><strong>UPDATE</strong>:</p> <p>I have not had any doubts that my logic is wrong but... I tried some things and with this code</p> <p>this is really weird. I changed now the logic of the LastDate and PreviousDate CanExecute method and both buttons work now changing the datepicker.</p> <pre><code>private bool CanMoveFirstDate() { Debug.WriteLine("SelectedDate FirstDate: " + SelectedDate); return SelectedDate &gt; MinDate; } private bool CanMovePreviousDate() { Debug.WriteLine("SelectedDate PreviousDate: " + SelectedDate); return SelectedDate &gt; MinDate; } </code></pre> <p>Is someone knows how to make the NextDate + LastDate button working gets the solution! :P</p> <p><strong>UPDATE 2:</strong></p> <p>Bindings are powerfull but maybe hard to control...</p> <p>I did some crazy logic shit again and now it seems to work:</p> <pre><code> private bool CanMoveNextDate() { Debug.WriteLine("SelectedDate NextDate: " + SelectedDate); return SelectedDate.AddDays(1) &lt; MaxDate; } private bool CanMoveLastDate() { Debug.WriteLine("SelectedDate LastDate: " + SelectedDate); return SelectedDate.AddDays(1) &lt; MaxDate; } </code></pre> <p>If someone can <strong>explain</strong> 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...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload