繁体   English   中英

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

[英]Update Binding in Windows Phone Runtime app

我正在尝试从Windows Phone运行时应用程序上的ListView修改某些项目。

通过简单的绑定将Items绑定到ListView:

this.defaultViewModel["myBinding"] = pi;

并在xaml中:

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

然后,我从代码修改绑定:

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

现在,我想用新修改的pi更新UI。 我知道this.defaultViewModel["myBinding"] = null; 然后this.defaultViewModel["myBinding"] = pi; 可以,但是它不会保留ListView的滚动位置(这样做后会跳到顶部)。

我也尝试过这个答案 ,但似乎UpdateTarget在Windows Phone RunTime应用程序中不可用。

因此,我该如何强制刷新ListView ItemsSource而不丢失其滚动位置?

您应该使用ObservableCollection<myItem>而不是List<myItem> 然后,您无需取消设置列表即可更新ListView。

若要滚动到具有ListView listView的ListView中的项目,可以调用listView.ScrollIntoView(item)

需要实现INofityPropertyChanged

MSDN:inotifypropertychanged示例

来自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