简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM