Hi,
Filtering ok on sfTreeGrid. But how do I bind to the record count after filtering?
I think I need to somehow hook up to the the treeGrid.View.RecordPropertyChanged?
Not sure how to do that as looks like the view is only created after setting the ItemSource in the ViewModel.
Thanks
Hi Lee Stevens,
We can get the records count after filtering was applied using “FilterChanged” event. Kindly
refer the following code snippets.
|
public MainWindow() { InitializeComponent(); this.treeGrid.FilterChanged += TreeGrid_FilterChanged; }
private void TreeGrid_FilterChanged(object? sender, Syncfusion.UI.Xaml.TreeGrid.Filtering.TreeGridFilterChangedEventArgs e) { var filteredRecords = this.treeGrid.View.Nodes.Count(); } |
Kindly provide the details, how you are tried to bind the record count after
filtering. It will help us to provide the better solution.
Thank you kindly for the reply.
I'm using WPF & MVC. I believe I won't have access to the treeGrid.View in my ViewModel from that event.
Also doesn't the view only get created after the datasource is set? Therfore I can't do anything in a behaviour either?
Hi Lee Stevens,
We
suggested using the FilterChanged event, as the TreeGrid view will be
accessible after loading the TreeGrid. However, you mentioned that you won't
have access to treeGrid.View.
To assist you better, could you kindly provide the following details.
Hi,
Ok on the Xaml we apply a behaviour:
<interactivity:Interaction.Behaviors>
<behaviors:TreeGridFilterBehaviour />
</interactivity:Interaction.Behaviors>
In the behaviour:
public class TreeGridFilterBehaviour : Behavior<SfTreeGrid>
{
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.Loaded += AssociatedObject_Loaded;
}
private void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
CompanyOverviewViewModel viewModel = this.AssociatedObject.DataContext as CompanyOverviewViewModel;
if (viewModel != null)
{
viewModel.filterChanged = OnFilterChanged;
THE VIEW IS NOT AVAILABLE HERE AS IT'S NOT CREATED UNTIL THE DATA SOURCE IS SET
//var treeGrid = this.AssociatedObject as SfTreeGrid;
//treeGrid.View.RecordPropertyChanged += viewModel.OnFilterChanged;
}
}
private void OnFilterChanged()
{
var treeGrid = this.AssociatedObject as SfTreeGrid;
var viewModel = this.AssociatedObject.DataContext as CompanyOverviewViewModel;
if (treeGrid.View != null)
{
treeGrid.View.Filter = viewModel.FilterRecords;
treeGrid.View.RefreshFilter();
}
}
Hi Lee Stevens,
We're able to access the treegrid.View and hooked the FilterChanged event in the Behavior. If this doesn't meet your requirements or if we've misunderstood your scenario, please provide details about your viewModel.
Please refer to the following code snippets.
|
public class TreeGridFilterBehaviour : Behavior<SfTreeGrid> { protected override void OnAttached() { base.OnAttached(); AssociatedObject.Loaded += AssociatedObject_Loaded; AssociatedObject.FilterChanged += AssociatedObject_FilterChanged; }
private void AssociatedObject_FilterChanged(object? sender, Syncfusion.UI.Xaml.TreeGrid.Filtering.TreeGridFilterChangedEventArgs e) { var filteredRecords = AssociatedObject.View.Nodes.Count(); }
private void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e) { var treeGrid = AssociatedObject as SfTreeGrid;
var viewModel = AssociatedObject.DataContext as ViewModel1; if (treeGrid.View != null) { treeGrid.View.Filter = viewModel.FilerNodes; treeGrid.View.RefreshFilter(); } } }
|
We have attached the sample for your reference. Please have a look at this.
Thank you for the excellent example.
Unfortuantley the event is not firing for us. Setup exactly as you have it.
I wonder if it's something to do with .net we are currently on 4.8 (slowly upgrading to .net 6!)
We might have to wait till that process is done.
Hi Lee Stevens,
This
issue might not be related to specific behaviors with .NET 6.0 or Framework
4.8. Kindly check our implementation and compare it with your application;
there may be a chance that something was missed. Please review and update us
with your concerns.