WPF:如何挂钩 ListView 的 ItemsSource CollectionChanged 通知?

发布于 2024-11-04 06:42:02 字数 290 浏览 6 评论 0原文

我有一个 ListView 数据绑定到 ObservableCollection ...

<ListView x:Name="List1" ItemsSource="{Binding MyList}" />

我似乎找不到集合更改时触发的任何事件,所以我想不知何故,我需要以某种方式连接到集合更改通知?我不太确定该怎么做。

基本上,当集合发生变化时,我想做的工作超出了 ListView 在更新其列表方面已经完成的工作。

I have a ListView that is databound to an ObservableCollection ...

<ListView x:Name="List1" ItemsSource="{Binding MyList}" />

I can't seem to find any event that are triggered when the collection changes, so I'm thinking that somehow I need to hook into the collectionchanged notification somehow? I'm not really sure how to do that.

Basically, when the collection changes I want to do additional work beyond what the ListView already does in updating it's list.

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

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

发布评论

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

评论(3

微凉徒眸意 2024-11-11 06:42:02

默认情况下,ItemsSource 的类型为 <代码>IEnumerable。您需要首先转换为可以访问 CollectionChanged 事件,然后为该事件添加处理程序。

((INotifyCollectionChanged)List1.ItemsSource).CollectionChanged +=
    new NotifyCollectionChangedEventHandler(List1CollectionChanged);

public void List1CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
{
    // Your logic here
}


Note: I cast it to INotifyCollectionChanged in my example, but you can really cast it to any object that implements that. Though, as a best practice, you should cast to the most generic type that gives you access to the methods/properties/events you need. So, while you can cast it to an ObservableCollection, you don't need to. INotifyCollectionChanged contains the event you need and if you ever decide to use some other type of collection that implements it, this will continue to work, whereas casting to an ObservableCollection means that if you one day decide that you're list is now of type MyOwnTypeOfObservableCollectionNotDerivedFromObservableCollection than this will break. ;)

PS 这应该放在 xaml 代码隐藏中。

By default the ItemsSource is of type IEnumerable. You need to first cast to a type that has access to the CollectionChanged event, then add a handler for that event.

((INotifyCollectionChanged)List1.ItemsSource).CollectionChanged +=
    new NotifyCollectionChangedEventHandler(List1CollectionChanged);

public void List1CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
{
    // Your logic here
}


Note: I cast it to INotifyCollectionChanged in my example, but you can really cast it to any object that implements that. Though, as a best practice, you should cast to the most generic type that gives you access to the methods/properties/events you need. So, while you can cast it to an ObservableCollection, you don't need to. INotifyCollectionChanged contains the event you need and if you ever decide to use some other type of collection that implements it, this will continue to work, whereas casting to an ObservableCollection means that if you one day decide that you're list is now of type MyOwnTypeOfObservableCollectionNotDerivedFromObservableCollection than this will break. ;)

P.S. This should go in the xaml code-behind.

不弃不离 2024-11-11 06:42:02

您必须将处理程序附加到您的列表中。或者,使用 < code>CollectionView 并在那里挂钩更改的事件。

在你的代码隐藏中,这样做:

MyList.CollectionChanged += new NotifyCollectionChangedEventHandler( this.MyCollectionChanged );


private void SortCollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
{
  Debug.WriteLine( "Changed" );
}

you are going to have to attach the handler to your list. Or, use a CollectionView and hook the changed event there.

in your codebehind, do like this:

MyList.CollectionChanged += new NotifyCollectionChangedEventHandler( this.MyCollectionChanged );


private void SortCollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
{
  Debug.WriteLine( "Changed" );
}
奶茶白久 2024-11-11 06:42:02

ObservableCollection{T} 公开 INotifyCollectionChanged.CollectionChanged< /a> 事件。当绑定到 ItemsSource 时,数据绑定引擎会处理从源到项目控件的更改,但如果您需要执行其他处理,您可以将处理程序附加到 CollectionChanged 事件并使用 NotifyCollectionChangedEventArgs 它提供了。

假设您的视图模型上有一个名为 MyList 的公共属性:

public ObservableCollection<T> MyList
{
  get
  {
    if(_viewModelMyList == null)
    {
      _viewModelMyList = new ObservableCollection<T>;
      _viewModelMyList.CollectionChanged += (o, e) => 
      {
        // code to process change event can go here
        if(e.Action == NotifyCollectionChangedAction.Add)
        {
        }
      };
    }
    return _viewModelMyList;
  }
}
private ObservableCollection<T> _viewModelMyList;

An ObservableCollection{T} exposes the INotifyCollectionChanged.CollectionChanged event. When binding to an ItemsSource the data binding engine handles the propogation of changes from the source to the items control, but if you need to perform additional processing you can attach a handler to the CollectionChanged event and use the NotifyCollectionChangedEventArgs it provides.

Assuming you have a public property on your view model named MyList:

public ObservableCollection<T> MyList
{
  get
  {
    if(_viewModelMyList == null)
    {
      _viewModelMyList = new ObservableCollection<T>;
      _viewModelMyList.CollectionChanged += (o, e) => 
      {
        // code to process change event can go here
        if(e.Action == NotifyCollectionChangedAction.Add)
        {
        }
      };
    }
    return _viewModelMyList;
  }
}
private ObservableCollection<T> _viewModelMyList;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文