开发者

C# Attach Property Control base class

开发者 https://www.devze.com 2022-12-10 01:52 出处:网络
I would like to know how to add a property from one of my user controls to the base controls property list.Is this possible?

I would like to know how to add a property from one of my user controls to the base controls property list. Is this possible?

So what I am doing is this:

private static List<LabelType> ConvertControlCollectionToList(Control customContainer)
    {
        LabelTypeList.LabelProps.Clear();

        foreach (Control c in customContainer.Controls)
        {
            LabelTypeList.LabelProps.Add(new LabelType());
            LabelTypeList.LabelProps.Last().Name = c.Name;
            LabelTypeList.LabelProps.Last().Top = c.Top;
            LabelTypeList.LabelProps.Last().Left = c.Left;
            LabelTypeList.LabelProps.Last().Height = c.Height;
            LabelTypeList.LabelProps.Last().Width = c.Width;
            LabelTypeList.LabelProps.Last().Font = c.Font;
            LabelTypeList.LabelProps.Last().Text = c.Text;
            LabelTypeList.LabelProps.Last().DataColumn = c.?????
            LabelTypeList.LabelProps.Last().Rotation = c.?????
        }

        return LabelTypeList.LabelProps;
    }

I have two properties at the end of the list which are custom to my usercontrol but I need to add them to the base control class so when I load the co开发者_开发知识库llection back into a form my settings for DataColumn and Rotation are accessible. Does that make sense?

Thanks.


Say, for example, that your control is a TextBox named txtFirstName and you want a property representing the text of that TextBox on your UserControl:

public string FirstName
{
    get { return txtFirstName.Text; }
    set { txtFirstName.Text = value; }
}

Edit

Given the edit to the question, no you can't do exactly what you're asking for (add your property to those of the base class). You can, however, check to see if a particular control is an instance of your UserControl, and gather those properties if it is. To do so, use this:

private static List<LabelType> ConvertControlCollectionToList(Control customContainer)
{
    LabelTypeList.LabelProps.Clear();

    foreach (Control c in customContainer.Controls)
    {
        LabelType lt = new LabelType();

        LabelTypeList.LabelProps.Add(lt);

        lt.Name = c.Name;
        lt.Top = c.Top;
        lt.Left = c.Left;
        lt.Height = c.Height;
        lt.Width = c.Width;
        lt.Font = c.Font;
        lt.Text = c.Text;

        YourUserControlType uc = c as YourUserControlType;

        if(uc != null)
        {
            lt.DataColumn = uc.DataColumn;
            lt.Rotation = uc.Rotation;
        }
    }

    return LabelTypeList.LabelProps;
}

(I removed the call to Last(), as that is a LINQ extension method and will cause the entire list to be traversed every time you set a property).


In WPF, you can use Attached Properties to provide this behavior. This is how things like Grid.Row works.

In Windows Forms, you'll want to use an Extender Provider.

Note that neither option truly adds properties to the base class - you can't change the base class structure in a subclass. They do add properties available at design time that are usable with the control.

0

精彩评论

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