BendEg
BendEg

Reputation: 21108

Override dependency property with new keyword

If i want to add some custom action when setting a dependency property in WPF, is it correct, to override it with the new keyword in c#?

For example, i want to add custom behaviour when setting the ItemsSource of a control:

public new object ItemsSource
{
    get
    {
        return base.ItemsSource;
    }
    set
    {
        handleSelectionChanged = true;
        base.ItemsSource = value;
    }
}

Can this cause any side effects? I am not sure if i did it the correct way.

Thank you!

Upvotes: 0

Views: 469

Answers (1)

xum59
xum59

Reputation: 851

As stated in comments, the new keyword will hide the inherited member and you might lose some functionnality (haven't done the test yet)

Here's a link explaining a few tips with Metadatas.

For your ItemsSource, I would suggest to attach a PropertyChangedCallback or a CoerceValueCallback to your initial property.

Upvotes: 1

Related Questions