0

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?

Community
  • 1
  • 1
Mahdi Ghiasi
  • 14,873
  • 19
  • 71
  • 119

2 Answers2

2

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).

dBlisse
  • 801
  • 5
  • 13
  • I'm sorry, but I can't get `ScrollIntoView` working. neither `myListView.ScrollIntoView(pi[7]);` nor `myListView.ScrollIntoView(myListView.Items[7]);` works. Can you help? – Mahdi Ghiasi Aug 18 '14 at 23:23
  • Try adding `listView.UpdateLayout()` before you call ScrollIntoView – dBlisse Aug 18 '14 at 23:35
  • 1
    Oh. I'm sorry. I missed your point about `ObservableCollection`. With the use of `ObservableCollection`, I don't need to set the binding to `null` and `pi` again. So I don't need `ScrollIntoView` at all. It does everything automatically. Thank you :) – Mahdi Ghiasi Aug 19 '14 at 00:25
1

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();
            }
        }
    }
}
Mahdi Ghiasi
  • 14,873
  • 19
  • 71
  • 119
karan
  • 3,319
  • 1
  • 35
  • 44