开发者

Add property to non user defined classes

We use controls all the time for example a button and s开发者_运维知识库ometimes we may want to add some custom properties to that button. For example numberOfTimesClicked etc. I am currently working with GridViewColumnHeader and I would like to add the property columnIndex to that class. I know I can keep track of the columns with other methods but I am just curios to know if we can add extra properties to classes. For example I was thinking about inheritance. If I create a new class and inherit from the class that I want to add new properties (in my case GridViewColumnHeader) then I think that should work but for some reason I get errors in my code whenever I do something like:

private class MyGridViewColumnHeader : GridViewColumnHeader
{
    public int propertyThatIWantToAdd { get; set; }
}

from now on if I instantiate objects from MyGridViewColumnHeader class instead of GridViewColumnHeader class I get errors. What is wrong with using inheritance to achieve this?


Instead of casting the sender object use

e.OriginalSource as MyGridViewColumnHeader

And do your work on that object. If you need to know what is inside the RoutedEventArgs have a look on MSDN


You should make the class public, the private modifier is only allowed for inner classes.

Also you should use Dependency Properties instead of normal Properties, if you want to access the property in XAML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜