How to raise an event when DataGrid.ItemsSource is changed

ItemsSource is a dependency property, so it’s easy enough to be notified when the property is changed to something else. You would want to use this in addition to code that you have, not instead of:

In Window.Loaded (or similar) you can subscribe like so:

var dpd = DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty, typeof(DataGrid));
if (dpd != null)
{
    dpd.AddValueChanged(myGrid, ThisIsCalledWhenPropertyIsChanged);
}

And have a change handler:

private void ThisIsCalledWhenPropertyIsChanged(object sender, EventArgs e)
{
}

Whenever the ItemsSource Property is set, the ThisIsCalledWhenPropertyIsChanged method is called.

You can use this for any dependency property you want to be notified about changes.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)