Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

c# - Prevent Winforms Designer from Generating Property Values for Inherited Controls

I have a custom DataGridView, let's say as such:

public MyGridView : DataGridView
{
    public MyGridView()
    {
         BackgroundColor = Color.Red;
    }
}

Now, when I use this control in a project using the designer, for some reason it feels a need to also set the property in the designer.cs file.

So in the designer file, I would have:

this.MyGridView1.BackgroundColor = System.Drawing.Color.FromArgb((byte)(int)255, (byte)(int)0, (byte)(int)0);

The problem me with this is that it prevents me from being able to change the color in the constructor of my MyGridView, without having to go through all the forms in which I used to control and change it per instance, rendering my custom control useless.

With some properties which offer a virtual getter, this is no problem, but most properties do not have it.

How can I prevent the designer from generating this code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I should emphasize that this isn't normally the way you do this, the [DefaultValue] attribute is normally the correct choice. But you are working with a property of type Color, it is not simple to write the attribute for that in a flexible way. The arguments you can pass to an attribute constructor can be only a select few data types, Color isn't one of them. You'd have to craft a string that ColorConverter can understand, that's both ugly and hard to maintain.

PropertyGrid has a secondary way of providing defaults for "difficult" properties, it will also look for specially named private members in the class. Given a property named "Xxxx", it looks for the following:

  • DefaultXxxx, a property with just a getter that returns the default value
  • ResetXxxx(), a method that can run when the user selects the Reset context menu item
  • ShouldSerializeXxxx(), a method that should return false if the value of the property should not be persisted.

Which makes this code work:

public class MyGridView : DataGridView {
    public MyGridView() {
        this.BackgroundColor = DefaultBackgroundColor;
    }
    public new Color BackgroundColor {
        get { return base.BackgroundColor; }
        set { base.BackgroundColor = value;  }
    }
    private bool ShouldSerializeBackgroundColor() {
        return !this.BackgroundColor.Equals(DefaultBackgroundColor);
    }
    private void ResetBackgroundColor() {
        this.BackgroundColor = DefaultBackgroundColor;
    }
    private static Color DefaultBackgroundColor {
        get { return Color.Red; }
    }
}

Note that the ResetBackgroundColor() method is not actually necessary since no special effects are required when the user resets the property, I just included it for completeness.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...