prevent的WinForms从生成的属性值设计为继承控件

prevent的WinForms从生成的属性值设计为继承控件

本文介绍了prevent的WinForms从生成的属性值设计为继承控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的的DataGridView ,让我们说这样:

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

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

现在,使用设计,当我使用这种控制在一个项目中,由于某种原因感到有必要也设置在designer.cs文件中的属性。

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);

这个问题我是它$ P $从能够改变颜色的构造pvents我,我的 MyGridView ,而不必通过所有的形式,我用于控制和每个实例改变它,使我的自定义控件没用。

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.

我怎样才能prevent从产生这个code的设计师?

How can I prevent the designer from generating this code?

推荐答案

我要强调,这不是通常这样做的方式, [默认值] 属性通常是正确的选择。但是,你正在使用Color类型的属性,它不是简单的写的属性以灵活的方式。你可以传递到属性构造函数的参数可以是只有一小部分的数据类型,颜色是不是其中之一。你必须制作一个的字符串的是ColorConverter能理解,这既难看又难维护。

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中具有提供默认值困难的属性的辅助方式,它也将寻找在类特别命名的私有成员。给定一个名为XXXX的属性,它会寻找以下内容:

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,只有一个getter一个属性,它返回的默认值
  • ResetXxxx(),当用户选择重置上下文菜单项,可以运行的方法
  • ShouldSerializeXxxx()方法应该返回的的如果财产的价值不应该被保留。
  • 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.

这使得这个code的工作:

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; }
    }
}

请注意,由于当用户重置属性是不需要特殊效果ResetBackgroundColor()方法实际上不是必要的,我只是把它列入的完整性。

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.

这篇关于prevent的WinForms从生成的属性值设计为继承控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 01:57