WPF 捕获 ObservableCollection 的项属性更改

发布于 2024-11-03 05:45:17 字数 1511 浏览 6 评论 0原文

您好,我尝试了几种方法来做到这一点,但要么不成功,要么不理想。我只是想 捕获集合中任何项目的 PropertyChanged 事件。我现在已经手动连接了 但我想知道是否有一个更优雅的解决方案:

public class Item : INotifyPropertyChanged
{
    ...
    public delegate void MyPropertyChangedHandler(object sender, PropertyChangedEventArgs e);
    public event MyPropertyChangedHandler MyPropertyChanged;

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);
        PropertyChanged(this, args);
        if (MyPropertyChanged != null) MyPropertyChanged(this, args);
    }
    ...
}

public class ItemCollection : ObservableCollection<Item>
{
    ...
    public delegate void MyPropertyChangedHandler(object sender, PropertyChangedEventArgs e);
    public event MyPropertyChangedHandler MyPropertyChanged;

    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (Item item in e.NewItems)
            {
                item.MyPropertyChanged += new Item.MyPropertyChangedHandler(item_MyPropertyChanged);
            }
        }

        base.OnCollectionChanged(e);
    }

    void item_MyPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        MyPropertyChanged(sender, e);
    }
    ...
}

然后我可以轻松地附加到集合的 MyPropertyChanged 事件,它工作得很好,但有人更优雅吗?

提前致谢

Hi I was trying a few ways of doing this but either wasn't successful or it wasn't ideal. I simply want to
catch the PropertyChanged event of any item in the collection. I have wired it up manually at the moment
but am wondering if there is a more elegant solution:

public class Item : INotifyPropertyChanged
{
    ...
    public delegate void MyPropertyChangedHandler(object sender, PropertyChangedEventArgs e);
    public event MyPropertyChangedHandler MyPropertyChanged;

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);
        PropertyChanged(this, args);
        if (MyPropertyChanged != null) MyPropertyChanged(this, args);
    }
    ...
}

public class ItemCollection : ObservableCollection<Item>
{
    ...
    public delegate void MyPropertyChangedHandler(object sender, PropertyChangedEventArgs e);
    public event MyPropertyChangedHandler MyPropertyChanged;

    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (Item item in e.NewItems)
            {
                item.MyPropertyChanged += new Item.MyPropertyChangedHandler(item_MyPropertyChanged);
            }
        }

        base.OnCollectionChanged(e);
    }

    void item_MyPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        MyPropertyChanged(sender, e);
    }
    ...
}

Then I can easily attach to the collection's MyPropertyChanged event and it works well but more elegant anyone?

Thanks in advance

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

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

发布评论

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

评论(1

半葬歌 2024-11-10 05:45:17

您几乎正在做您需要做的事情,据我所知,没有更好的方法可以做到这一点。

有一件事是,您可能需要添加代码以在从集合中删除项目时删除事件处理程序,以成为一个好公民并避免内存泄漏。

you are pretty much doing what you need to do, AFIK there is no better way to do this.

one thing, you might want to add code to remove your event handler when the item is removed from the collection, to be a good citizen and to avoid memory leaks.

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