Earlgray
Earlgray

Reputation: 647

WP7 - Refresh ListBox on navigation GoBack()

I have a MainPage.xaml where is ListBox and Button. When I click on the button then MainPage is navigated to AddPage.xaml. This page is for adding new items, there are two TextBoxes and submit Button. When I click on that submit Button,then data from TextBoxes are saved to XML file and then is called GoBack().

I need to refresh ListBox in my MainPage.xaml when Im going back from AddPage.xaml, but it doesnt work automaticly. How can I do that?

My MainViewModel.cs

public class MainViewModel : INotifyPropertyChanged
{
    public ObservableCollection<Context> Contexts { get; private set; }

    public MainViewModel()
    {
        this.Contexts = new ObservableCollection<Context>();
    }

    public bool IsDataLoaded
    {
        get;
        private set;
    }

    public void LoadData()
    {
        try
        {
            var file = IsolatedStorageFile.GetUserStoreForApplication();
            XElement xElem;

            using (IsolatedStorageFileStream read = file.OpenFile("contexts.xml", FileMode.Open))
            {
                xElem = XElement.Load(read);
            }

            var contexts = from context in xElem.Elements("Context")
                           orderby (string)context.Element("Name")
                           select context;

            foreach (XElement xElemItem in contexts)
            {
                Contexts.Add(new Context
                {
                    Name = xElemItem.Element("Name").Value.ToString(),
                    Note = xElemItem.Element("Note").Value.ToString(),
                    Created = xElemItem.Element("Created").Value.ToString()
                });
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        this.IsDataLoaded = true;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

and Context.cs

public class Context : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (value != _name)
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }

    private string _note;
    public string Note
    {
        get
        {
            return _note;
        }
        set
        {
            if (value != _note)
            {
                _note = value;
                NotifyPropertyChanged("Note");
            }
        }
    }

    private string _created;
    public string Created
    {
        get
        {
            return _created;
        }
        set
        {
            if (value != _created)
            {
                _created = value;
                NotifyPropertyChanged("Created");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Upvotes: 0

Views: 831

Answers (2)

Caleb Keith
Caleb Keith

Reputation: 816

Tip: You aren't raising a property changed notification for your View Model's properties.

On the load event of the MainPage, call LoadData. You should also clear the observable collection when you call the LoadData method before adding anything to it because simply loading the data will cause duplicate entries in your collection.

Upvotes: 0

Matt Lacey
Matt Lacey

Reputation: 65586

You'll need to tell the main page that there is new data to reload.
At it's simplest, something like this:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (e.NavigationMode == NavigationMode.Back)
    {
        (this.DataContext as MainViewModel).LoadData();
    }
}

Upvotes: 1

Related Questions