Mahdi Ghiasi
Mahdi Ghiasi

Reputation: 15311

Update Binding in Windows Phone Runtime app

I'm trying to modify some items from a ListView on my Windows Phone RunTime app.

The Items are binded to the ListView by a simple binding:

this.defaultViewModel["myBinding"] = pi;

and in xaml:

<ListView ItemsSource="{Binding myBinding}" ... >

Then, I'm modifying the binding from code:

        List<myItem> pi = (List<myItem>)this.defaultViewModel["myBinding"];
        pi.RemoveAt(5);

Now, I want to update the UI with the new modified pi. I know that this.defaultViewModel["myBinding"] = null; and then this.defaultViewModel["myBinding"] = pi; works, but it doesn't keep the scroll position of ListView (it jumps to top after doing this).

Also I have tried this answer, but seems that UpdateTarget is not available in Windows Phone RunTime apps.

So, what should I do to force refresh ItemsSource of my ListView, without losing it's scroll position?

Upvotes: 0

Views: 1518

Answers (2)

karan
karan

Reputation: 3369

Need to Implement INofityPropertyChanged

MSDN: inotifypropertychanged Example

Sample from the MSDN article:

public class DemoCustomer : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private DemoCustomer()
    {
    }

    private string customerNameValue = String.Empty;
    public string CustomerName
    {
        get
        {
            return this.customerNameValue;
        }

        set
        {
            if (value != this.customerNameValue)
            {
                this.customerNameValue = value;
                NotifyPropertyChanged();
            }
        }
    }
}

Upvotes: 1

dBlisse
dBlisse

Reputation: 811

You should be using ObservableCollection<myItem> instead of List<myItem>. Then you won't need to unset and set the list to update the ListView.

To scroll to an item in a ListView with a ListView listView, you can call listView.ScrollIntoView(item).

Upvotes: 2

Related Questions