English 中文(简体)
WPF:我如何看一看收到的文件一览表?
原标题:WPF: How do I hook into a ListView s ItemsSource CollectionChanged notification?

我有<代码>ListView,该编码为<编码>上的数据。 ObservableCollection.

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

在收集工作发生变化时,我似乎无法发现任何触发事件,因此我认为,我在某种程度上需要忽略所换的通知? 我并不真正确定如何做到这一点。

基本上,当收件发生变化时,我想在清单更新清单方面所做的大量工作之外再做一些工作。

最佳回答

> 你们需要首先投放到能够进入>>>.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. ;)

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

问题回答

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

在你看来,这样做是:

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


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

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.

假设你认为你有公有财产,这种模式的名称是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;




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签