Note that there are some explanatory texts on larger screens.

plurals
  1. POCalendar binding SelectedDate and DisplayDate: gray dates
    primarykey
    data
    text
    <p><strong>The original use case:</strong></p> <p>This is a possible use case I'm trying to solve:</p> <ol> <li>The user opens the page with my calendar set to the now date</li> <li>User selects a date a few months back</li> <li>User refreshes the page, causing the selected date in the VM to be reset to the now date</li> </ol> <p><em>Expected:</em> The calendar displays the now date</p> <p><em>Actual:</em> (with binding only on SelectedDate) The calendar only moves its selected date, but keeps showing the previously selected month</p> <p><strong>My solution:</strong></p> <p>In an MVVM scenario I am showing a <code>Calendar</code> control whose SelectedDate has a binding to a <code>SelectedDate</code> property in my ViewModel. The property in my VM triggers it's <code>PropertyChanged</code> event when set.</p> <p>The selected date in my VM can also be set from other parts in my code, so I want my calendar to always show the selected date (for example when another month is set as the selected date, I want my calendar to switch to that month). To get this behavior I also bound the <code>DisplayDate</code> property of my calendar to my SelectedDate property, and this gives me the expected behavior, except when I click on the gray dates of the previous or next month.</p> <p>This is my Calendar in XAML:</p> <pre class="lang-xml prettyprint-override"><code>&lt;Calendar SelectedDate="{Binding Path=SelectedDate}" DisplayDate="{Binding Path=SelectedDate}" /&gt; </code></pre> <p>This the <code>SelectedDate</code> property in my ViewModel (implements <code>INotifyPropertyChanged</code>)</p> <pre class="lang-cpp prettyprint-override"><code> public DateTime SelectedDate { get { return selectedDate; } set { selectedDate = value; this.PropertyChanged(this, new PropertyChangedEventArgs("SelectedDate")); } } </code></pre> <p><strong>The problem with the solution:</strong></p> <p>Apparently clicking on a gray date causes the <code>SelectedDate</code> to be set on mouse down and again on mouse up, which causes a selection of a date two months away.</p> <p>Is there a better way to implement this kind of behavior? Or can I fix the DisplayDate problem (without too much hacking, meaning no/as little code as possible in code behind)?</p> <p>All I really want is a <code>SelecteDate</code> property which also has the behavior of the <code>DisplayDate</code> property :)</p> <p>Thank you for your help!</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