我有<代码>ListView,该编码为<编码>上的数据。 ObservableCollection.
<ListView x:Name="List1" ItemsSource="{Binding MyList}" />
在收集工作发生变化时,我似乎无法发现任何触发事件,因此我认为,我在某种程度上需要忽略所换的通知? 我并不真正确定如何做到这一点。
基本上,当收件发生变化时,我想在清单更新清单方面所做的大量工作之外再做一些工作。
我有<代码>ListView,该编码为<编码>上的数据。 ObservableCollection.
<ListView x:Name="List1" ItemsSource="{Binding MyList}" />
在收集工作发生变化时,我似乎无法发现任何触发事件,因此我认为,我在某种程度上需要忽略所换的通知? 我并不真正确定如何做到这一点。
基本上,当收件发生变化时,我想在清单更新清单方面所做的大量工作之外再做一些工作。
((INotifyCollectionChanged)List1.ItemsSource).CollectionChanged +=
new NotifyCollectionChangedEventHandler(List1CollectionChanged);
public void List1CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
{
// Your logic here
}
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;
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 ...
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?...
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 ...
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 ...
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 ...
I ve got some code which sets up a datacontext. Often enough, the datacontext should be set to some underlying data collection, such as an ObservableCollection - but occasionally I d like to set it ...
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 ...
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 ...