开发者

How to add functionality to an inherited property?

I'm following the instructions at this post: how to create Multiple user control that pointing single code behind file in silverlight 4

This is an example from my code.

public class MyCommonUserControl : UserControl
{
    public static DependencyProperty CustomProperty = DependencyProperty.Register(
        "CustomProperty", typeof(int), typeof(MyCommonUserControl));

     public int CustomProperty
     {
         get
         {  
             return (int)GetValue(TypeProperty);
         }
         set
         {
             SetValue(TypeProperty, value);
             Extension();
         }
     }

     public virtual void Extension()
     {
     }
}

public class FirstMyCommonUserControl : MyCommonUserControl 
{
     public FirstMyCommonUserControl()
     {
         InitializeComponent();
     }

     public override void Extension()
     {
         base.Extension();

         // Do something
     }
}

As you can see above, I'm using inheritance. This code works for me, because I've got several custom controls, one of them is FirstMyCommonUserControl class which is overriding the virtual method and I can add some stuff for this particular class. Then I do this:

    MyCommonUserControl A;
    int number;

    private void button1_Click(object sender, RoutedEventArgs e)
    {            
        switch (number)
        {
            case 1:
            case 2:
            case 3:
            case 4:
                A = new FirstMyCommonUserControl();
                A.CustomProperty = number;

                canvas1.Children.Add((FirstMyCommonUserControl)A);
                break;
            case 5:
            case 6:
            case 7:
            case 8:
                A = new AnotherMyCommonUserControl();
                A.CustomProperty = number;

                canvas1.Children.Add((AnotherMyCommonUserControl)A);
                break;
        }
    }

Each particular class needs to do something more in CustomPropert开发者_JAVA技巧y. I made it using virtual methods and overriding it. I don't know if doing this is the best way.


Properties can also be virtual, not only methods. So you could write something like

public class MyCommonUserControl : UserControl
{     
    public virtual int CustomProperty     
    {         
        get { return (int)GetValue(TypeProperty); }
        set { SetValue(TypeProperty, value); }
    }
}

public class FirstMyCommonUserControl : MyCommonUserControl 
{
    public override int CustomProperty     
    {         
        get 
        { 
            // Do something
            return base.CustomProperty;
        }
        set 
        { 
            // Do something
            base.CustomProperty = value;
        }
    }
}

Although whether or not is this better is debatable.

Also, there is no need for a cast in this expression of yours:

canvas1.Children.Add((FirstMyCommonUserControl)A);

You can just write:

canvas1.Children.Add(A);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜