首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Windows运行时应用程序中的更新绑定

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

Stack Overflow用户
提问于 2014-08-18 23:02:52
回答 2查看 1.5K关注 0票数 0

我试图在我的Windows ListView应用程序中修改RunTime中的一些项目。

这些项通过简单的绑定绑定到ListView:

代码语言:javascript
运行
复制
this.defaultViewModel["myBinding"] = pi;

在xaml中:

代码语言:javascript
运行
复制
<ListView ItemsSource="{Binding myBinding}" ... >

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

代码语言:javascript
运行
复制
        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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-18 23:15:00

您应该使用ObservableCollection<myItem>而不是List<myItem>。然后,您将不需要取消设置和设置列表来更新ListView。

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

票数 2
EN

Stack Overflow用户

发布于 2015-06-26 06:31:38

需要实现INofityPropertyChanged

MSDN: inotifypropertychanged变更示例

MSDN文章中的示例:

代码语言:javascript
运行
复制
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();
            }
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25373377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档