簡體   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