简体   繁体   English

Windows Phone运行时应用程序中的更新绑定

[英]Update Binding in Windows Phone Runtime app

I'm trying to modify some items from a ListView on my Windows Phone RunTime app. 我正在尝试从Windows Phone运行时应用程序上的ListView修改某些项目。

The Items are binded to the ListView by a simple binding: 通过简单的绑定将Items绑定到ListView:

this.defaultViewModel["myBinding"] = pi;

and in xaml: 并在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 . 现在,我想用新修改的pi更新UI。 I know that this.defaultViewModel["myBinding"] = null; 我知道this.defaultViewModel["myBinding"] = null; and then this.defaultViewModel["myBinding"] = pi; 然后this.defaultViewModel["myBinding"] = pi; works, but it doesn't keep the scroll position of ListView (it jumps to top after doing this). 可以,但是它不会保留ListView的滚动位置(这样做后会跳到顶部)。

Also I have tried this answer , but seems that UpdateTarget is not available in Windows Phone RunTime apps. 我也尝试过这个答案 ,但似乎UpdateTarget在Windows Phone RunTime应用程序中不可用。

So, what should I do to force refresh ItemsSource of my ListView , without losing it's scroll position? 因此,我该如何强制刷新ListView ItemsSource而不丢失其滚动位置?

You should be using ObservableCollection<myItem> instead of List<myItem> . 您应该使用ObservableCollection<myItem>而不是List<myItem> Then you won't need to unset and set the list to update the ListView. 然后,您无需取消设置列表即可更新ListView。

To scroll to an item in a ListView with a ListView listView , you can call listView.ScrollIntoView(item) . 若要滚动到具有ListView listView的ListView中的项目,可以调用listView.ScrollIntoView(item)

Need to Implement INofityPropertyChanged 需要实现INofityPropertyChanged

MSDN: inotifypropertychanged Example MSDN:inotifypropertychanged示例

Sample from the MSDN article: 来自MSDN文章的示例:

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();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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