我试图在我的Windows ListView
应用程序中修改RunTime中的一些项目。
这些项通过简单的绑定绑定到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应用程序中是不可用的。
那么,如何在不丢失ListView
的滚动位置的情况下强制刷新ListView
?
发布于 2014-08-18 23:15:00
您应该使用ObservableCollection<myItem>
而不是List<myItem>
。然后,您将不需要取消设置和设置列表来更新ListView。
若要滚动到具有ListView的ListView listView
中的项,可以调用listView.ScrollIntoView(item)
。
发布于 2015-06-26 06:31:38
需要实现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();
}
}
}
}
https://stackoverflow.com/questions/25373377
复制相似问题