简体   繁体   中英

Windows Phone: refresh a binded listbox “manually”

the question seems to be easy, but I didn't find responses to this question.

I have my list of items working perfectly, binded to my MVVM. When I update an element, all is coordinated well, changes are reflected, etc.

One of my fields is calculated depending on the current day. So, if the user press HOME and exits the App, and tomorrow he comes back, the list is not refreshed, it is showing the previous day data.

To resolve this, I thought in use the OnNavigatedTo and OnNavigatedFrom events, saving the "entrance" day at start and compare it with the current day in the OnNavigatedTo event (which is fired when resuming the App). Detecting this day change, I could refresh the list explicity.

The question is, how I refresh the list? Or maybe I'm complicating the things a bit and there is a better way to do this.

EDIT: Final solution.

For those who need the same functionality, here is the solution I found:

    // Declare this var in the MainPage class
    // Holds the starting app day. If when going back to this page it has changed, refresh the list
    private DateTime loadDate;

    // Save the current day. If when going back to here it has changed, refresh the list
    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        loadDate = DateTime.Today;
    }

    // Read the current day and compare with saved. If when going back to here it has changed, refresh the list
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        // Read the current day and compare
        if (loadDate != DateTime.Today)
        {
            // The day has changed. Loop the list to refresh every item
            foreach (Item item in listBoxControl.Items)
            {
                item.CalculateMyOwnFieldNotBindedToDB();
            }
        }
    }

The problem is if you raise property change on a List without actually changing the List, it will be actually ignored because the view will detect that the List object has actually not changed. One workaround for that could be to just set the List to null and then set it back to the original List.
Another solution would be to just loop through the items in the List and Raise a property change just on your date field like this it will not require the whole list to be refreshed just the actual property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM