Maestro
Maestro

Reputation: 9508

How to know when binding is completed?

When I set the .ItemSource() property on a DataGrid to a Collection, the call returns fast, but the actual binding happens afterwards. Since I want to display a waiting cursor, I need to detect when the actual binding has finished. Is there any event for this?

Upvotes: 1

Views: 2341

Answers (3)

Incofab
Incofab

Reputation: 11

Your best bet is to hook into OnPropertyChanged event in your Window or User Control. This event is fired every time a property is updated. Then check for the actual property you wish to observe and take action.

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        if ("YOUR_PROPERTY_NAME".Equals(e.Property.ToString()))
        {
            // Take action
        }
        base.OnPropertyChanged(e);
    }

Upvotes: 0

Aleksandar
Aleksandar

Reputation: 4144

I waited for my DataGrid's Loaded event to fire, and I did a BeginInvoke, like this:

private void SubjectsList_Loaded(object sender, RoutedEventArgs e)
{
    Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => ColorMyRows()));
}

More details available in my answer here: https://stackoverflow.com/a/44464630/2101117

Upvotes: 0

Bill Reiss
Bill Reiss

Reputation: 3460

Anything based on ItemsControl uses an ItemContainerGenerator to generate its items in the background. You can access the ItemContainerGenerator property of the DataGrid and hook up the StatusChanged event to determine when it's done. If you're using virtualization and scroll, this will fire again so you need to handle that if necessary in your case.

Upvotes: 1

Related Questions