如何更新WPF ListView的源?

发布于 2024-10-04 07:22:56 字数 154 浏览 5 评论 0原文

我有一个 WPF ListView 来绑定我的集合。该集合中对象的属性在后台线程中更改。当属性更改时,我需要更新 ListView。当我更改某些对象的属性时,不会触发 SourceUpdated 事件。

PS 将 ItemSource 设置为 null 并重新绑定是不合适的。

I have a WPF ListView to wich I bind my collection. Properties of the objects in this collection are changed in a background thread. I need to update ListView when properties are changed. SourceUpdated event is not fired when I change some object's property.

P.S. Setting ItemSource to null and rebinding then is not appropriate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味超标? 2024-10-11 07:22:56

这应该是自动的,您只需要使用 ObservableCollection 作为对象的容器,并且对象的类需要实现 INotifyPropertyChanged (您可以只实现要通知列表视图发生更改的属性的模式)

MSDN

that should be automatic, you just need to use an ObservableCollection as a container for your objects, and your object's class needs to implement INotifyPropertyChanged (you can just implement the pattern for the properties which you want to notify the listview that there was a change)

MSDN

久伴你 2024-10-11 07:22:56

确保您的对象实现 INotifyPropertyChanged 并在您的属性上调用 setter 时引发所需的更改通知。

// This is a simple customer class that 
// implements the IPropertyChange interface.
public class DemoCustomer  : INotifyPropertyChanged
{
    // These fields hold the values for the public properties.
    private Guid idValue = Guid.NewGuid();
    private string customerNameValue = String.Empty;
    private string phoneNumberValue = String.Empty;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    // The constructor is private to enforce the factory pattern.
    private DemoCustomer()
    {
        customerNameValue = "Customer";
        phoneNumberValue = "(555)555-5555";
    }

    // This is the public factory method.
    public static DemoCustomer CreateNewCustomer()
    {
        return new DemoCustomer();
    }

    // This property represents an ID, suitable
    // for use as a primary key in a database.
    public Guid ID
    {
        get
        {
            return this.idValue;
        }
    }

    public string CustomerName
    {
        get
        {
            return this.customerNameValue;
        }

        set
        {
            if (value != this.customerNameValue)
            {
                this.customerNameValue = value;
                NotifyPropertyChanged("CustomerName");
            }
        }
    }

    public string PhoneNumber
    {
        get
        {
            return this.phoneNumberValue;
        }

        set
        {
            if (value != this.phoneNumberValue)
            {
                this.phoneNumberValue = value;
                NotifyPropertyChanged("PhoneNumber");
            }
        }
  }
}

如果您指的是从集合中添加/删除的项目(您没有提及),那么您需要确保您的集合是一个 ObservableCollection

Make sure your object implements INotifyPropertyChanged and raises the required change notification when the setter is called on your property.

// This is a simple customer class that 
// implements the IPropertyChange interface.
public class DemoCustomer  : INotifyPropertyChanged
{
    // These fields hold the values for the public properties.
    private Guid idValue = Guid.NewGuid();
    private string customerNameValue = String.Empty;
    private string phoneNumberValue = String.Empty;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    // The constructor is private to enforce the factory pattern.
    private DemoCustomer()
    {
        customerNameValue = "Customer";
        phoneNumberValue = "(555)555-5555";
    }

    // This is the public factory method.
    public static DemoCustomer CreateNewCustomer()
    {
        return new DemoCustomer();
    }

    // This property represents an ID, suitable
    // for use as a primary key in a database.
    public Guid ID
    {
        get
        {
            return this.idValue;
        }
    }

    public string CustomerName
    {
        get
        {
            return this.customerNameValue;
        }

        set
        {
            if (value != this.customerNameValue)
            {
                this.customerNameValue = value;
                NotifyPropertyChanged("CustomerName");
            }
        }
    }

    public string PhoneNumber
    {
        get
        {
            return this.phoneNumberValue;
        }

        set
        {
            if (value != this.phoneNumberValue)
            {
                this.phoneNumberValue = value;
                NotifyPropertyChanged("PhoneNumber");
            }
        }
  }
}

If you are instead referring to items being added/removed from the collection (which you did not mention) then you would need to make sure your collection is an ObservableCollection<T>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文