How to refresh the data in pivot item after updati

2019-02-25 15:07发布

问题:

Hello all i am working on windows phone 8 app, i am facing a issue, i am loading some data in my pivot item and if user tap a user control opens and i modify the data through user control. Data is saving into the database successfully but my pivot item is not updating immediately. i am using observable collection as following.

ObservableCollection i used like following in mypivot.xaml.cs file

ObservableCollection<MyWrapper> saveinfo = new ObservableCollection<MyWrapper>();
public ObservableCollection<MyWrapper> savedWordBankCollection
{ get { return saveinfo; } }

//MyWrapper class structure

public class MyWrapper: INotifyPropertyChanged
{
    private string desc;
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyChange(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
    public string Name { get; set; }
    public string NameDescription
    {
        get { return desc; }
        set
        {
            desc = value;
            NotifyChange(new PropertyChangedEventArgs("NameDescription"));
        }
    }
    public string NameId { get; set; }   
    public string NameLocId { get; set; }
}

Now as following i am loading data into my pivot item in pivot page

private void LoadWordbank()
{
        List<MysecondWrapper> dbData = helper.FetchAllName(thisApp.CurrentName.Id);
    if (dbData.Count != 0)
    {
        foreach (MySerconWrapper item in dbData)
        {
            saveinfo.Add(new MyWrapper { NameLocalId = item.Id.ToString(), Name= item.Name, NameDescription = item.Description, NameId = thisApp.CurrentName.Id});
        }
    }
}

mypivot.xaml as follwoing. i am not writing full code but how i have assigned the attributes that i am showing.

 <TextBlock x:Name="wordbankStored" Grid.Column="0" Grid.Row="0" Text="{Binding Name}"/>                               
 <Button x:Name="btnWordDescription" Grid.Row="1" Grid.Column="0" Content="{Binding NameDescription}" 
Tag="{Binding}" Click="btnNameDescription_Click"/>

In above textblocks i tried:

Content="{Binding NameDescription, Mode=TwoWay}"

but it didn't work so i have removed. on btnNameDescription_Click my user control opens and i can save data in my local db of wp8 but it does not show immediately in my pivot. Please give me suggession what and how to do ? where i am wrong. need help.

回答1:

I have done, first of all no need to refresh the page, observable collection can do it automatically. my observableCollection is saveInfoCollection.

there are three possiblities with the observable collection

1) deletion a item from observable collection.

2) Modifying a item in observable collection.

3) Add a item in observable collection.

Explaination

1) In first case when i will delete the item from the observable collection i will use the remove method of the observable collection like following

//savedData is my observableCollection name.
savedData.Remove(selected);

2) In second case when i will modify the item from the observable collection, here you will see the magic of the observable collection, I am taking the item object with the help of Tag property, so as i will update my database it my observable collection will automatically update.

3) In this case you can add new data object into the observable collection, and it will automatically update the observable collection.

If you are using the observableCollection than no need to refresh the page. It is the magic of ViewModel.