C# 在Windows Phone运行时应用程序中更新绑定

C# 在Windows Phone运行时应用程序中更新绑定,c#,listview,windows-runtime,windows-phone-8.1,C#,Listview,Windows Runtime,Windows Phone 8.1,我正在尝试修改Windows Phone运行时应用程序上列表视图中的某些项目 项目通过简单的绑定绑定到ListView: this.defaultViewModel["myBinding"] = pi; 在xaml中: <ListView ItemsSource="{Binding myBinding}" ... > 然后,我将修改代码中的绑定: List<myItem> pi = (List<myItem>)this.defaultV

我正在尝试修改Windows Phone运行时应用程序上
列表视图
中的某些项目

项目通过简单的绑定绑定到ListView:

this.defaultViewModel["myBinding"] = pi;
在xaml中:

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

然后,我将修改代码中的绑定:

        List<myItem> pi = (List<myItem>)this.defaultViewModel["myBinding"];
        pi.RemoveAt(5);
List pi=(List)this.defaultViewModel[“myBinding”];
pi.RemoveAt(5);
现在,我想用新修改的
pi
更新UI。我知道
this.defaultViewModel[“myBinding”]=null
然后
this.defaultViewModel[“myBinding”]=pi
可以工作,但它不会保持ListView的滚动位置(执行此操作后,它会跳到顶部)

我也尝试过,但似乎在Windows Phone运行时应用程序中没有
UpdateTarget


那么,我应该怎么做才能强制刷新我的
列表视图的
ItemsSource
,而不丢失它的滚动位置呢?

您应该使用
observeCollection
而不是
List
。这样,就不需要取消设置和设置列表来更新ListView


要滚动到具有
列表视图列表视图的列表视图中的项目,您可以调用
列表视图。滚动到视图(项目)

,您应该使用
可观察到的集合
,而不是
列表
。这样,就不需要取消设置和设置列表来更新ListView


要滚动到具有
列表视图列表视图的列表视图中的项目,您可以调用
列表视图。滚动到视图(项目)

,您应该使用
可观察到的集合
,而不是
列表
。这样,就不需要取消设置和设置列表来更新ListView


要滚动到具有
列表视图列表视图的列表视图中的项目,您可以调用
列表视图。滚动到视图(项目)

,您应该使用
可观察到的集合
,而不是
列表
。这样,就不需要取消设置和设置列表来更新ListView


要滚动到具有
列表视图列表视图的列表视图中的项目,可以调用
列表视图。滚动到视图(项目)
需要实现INofityPropertyChanged

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

需要实施INofityPropertyChanged

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

需要实施INofityPropertyChanged

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

需要实施INofityPropertyChanged

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

很抱歉,我无法使
ScrollIntoView
工作。无论是
myListView.ScrollIntoView(pi[7])
myListView.ScrollIntoView(myListView.Items[7])工作。您能帮忙吗?在调用ScrollingToViewOh之前,请尝试添加
listView.UpdateLayout()
。我很抱歉。我没有领会你关于
可观察到的收集
的观点。使用
observateCollection
,我不需要再次将绑定设置为
null
pi
。所以我根本不需要
滚动查看
。它能自动完成一切。谢谢:)很抱歉,我无法让
ScrollIntoView
工作。无论是
myListView.ScrollIntoView(pi[7])
myListView.ScrollIntoView(myListView.Items[7])工作。您能帮忙吗?在调用ScrollingToViewOh之前,请尝试添加
listView.UpdateLayout()
。我很抱歉。我没有领会你关于
可观察到的收集
的观点。使用
observateCollection
,我不需要再次将绑定设置为
null
pi
。所以我根本不需要
滚动查看
。它能自动完成一切。谢谢:)很抱歉,我无法让
ScrollIntoView
工作。无论是
myListView.ScrollIntoView(pi[7])
myListView.ScrollIntoView(myListView.Items[7])工作。您能帮忙吗?在调用ScrollingToViewOh之前,请尝试添加
listView.UpdateLayout()
。我很抱歉。我没有领会你关于
可观察到的收集
的观点。使用
observateCollection
,我不需要再次将绑定设置为
null
pi
。所以我根本不需要
滚动查看
。它能自动完成一切。谢谢:)很抱歉,我无法让
ScrollIntoView
工作。无论是
myListView.ScrollIntoView(pi[7])
myListView.ScrollIntoView(myListView.Items[7])工作。您能帮忙吗?在调用ScrollingToViewOh之前,请尝试添加
listView.UpdateLayout()
。我很抱歉。我没有领会你关于
可观察到的收集
的观点。使用
observateCollection
,我不需要再次将绑定设置为
null
pi
。所以我根本不需要
滚动查看
。它能自动完成一切。谢谢:)