We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Can't connect to CollectionChanged

I am using 15.3.0.26 WPF SfDataGrid in MVVM application.

In response to a click from the user, the viewmodel needs the filtered records from the SfDataGrid. Using FilterChanged event, I can get the filtered records when the filter changes. However, the underlying collection might change (and change the filtered record set) between the time the filter was changed and when I need the filtered records.

I would like to use the CollectionChanged event, but I can't register a handler for it. In https://www.syncfusion.com/forums/120981/event-that-triggered-when-added-new-row, it shows adding the CollectionChanged handler during the Loaded event handling. In my case, the SfDataGrid.View property is null when datagrid_Loaded is called, so I can't add the new CollectionChanged handler.

How can I handle the CollectionChanged event?

It would be even better if I could do this in XAML. I've tried the following, but (perhaps not surprisingly) the command never gets executed.

<i:Interaction.Triggers>
     <i:EventTrigger EventName="CollectionChanged">
          <i:InvokeCommandAction Command="{Binding CollectionChangedCommand}"
                                             CommandParameter="{Binding ElementName=sfgrid }" />
     </i:EventTrigger>
</i:Interaction.Triggers>


1 Reply

GT Gnanasownthari Thirugnanam Syncfusion Team September 4, 2017 04:41 PM UTC

Hi Brian, 

On analyzing your query, you can’t directly bind the collectionchanged event in XAML as like your given code snippet but you can listen the events of View from BindableView property setter as like below code example. 

<Syncfusion:SfDataGrid x:Name="datagrid"   
                               AllowDeleting="True" 
                               AllowEditing="True"                                
                               AutoGenerateColumns="False"    
                               BindableView="{Binding View,Mode=TwoWay}" 
                               ItemsSource="{Binding OrderInfoCollection }"> 

C# 
private ICollectionViewAdv view; 
public ICollectionViewAdv View 
{ 
    get 
    { 
        return view; 
    } 
    set 
    { 
        view = value; 
        //You can listen the view events from here. 
        if(view!=null) 
            view.CollectionChanged += View_CollectionChanged; 
    } 
} 
 
private void View_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
{ 
    //You can do your own logic here. 
} 

We have prepared the sample based on your requirement, please find the sample from the below link and let us know if this helps you. 

Sample location: 
 
Regards, 
Gnanasownthari T. 
 


Loader.
Live Chat Icon For mobile
Up arrow icon