Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建从父对象继承的环境属性';s属性_C#_.net_Vb.net_Winforms_Windows Forms Designer - Fatal编程技术网

C# 创建从父对象继承的环境属性';s属性

C# 创建从父对象继承的环境属性';s属性,c#,.net,vb.net,winforms,windows-forms-designer,C#,.net,Vb.net,Winforms,Windows Forms Designer,我似乎不知道该怎么做。我有一个继承的控件:MyControl,其属性名为MyOtherFont。如何让MyOtherFont继承父控件字体属性的环境值 例如,从设计器中,如果我将此控件拖到字体为Segoe UI的窗体上,它应该从窗体继承该值,而不是在属性窗口中以粗体显示 谢谢把它弄明白了。下面是一个C#示例,它完全符合我的示例所描述的。希望这对别人有帮助 public class MyControl : Control { private Font myOtherFont; p

我似乎不知道该怎么做。我有一个继承的控件:MyControl,其属性名为MyOtherFont。如何让MyOtherFont继承父控件字体属性的环境值

例如,从设计器中,如果我将此控件拖到字体为Segoe UI的窗体上,它应该从窗体继承该值,而不是在属性窗口中以粗体显示


谢谢

把它弄明白了。下面是一个C#示例,它完全符合我的示例所描述的。希望这对别人有帮助

public class MyControl : Control
{

    private Font myOtherFont;
    public Font MyOtherFont
    {
        get
        {
            if (this.myOtherFont == null)
            {
                if (base.Parent != null)
                    return base.Parent.Font;
            }

            return this.myOtherFont;
        }
        set
        {
            this.myOtherFont = value;
        }
    }

    private bool ShouldSerializeMyOtherFont()
    {
        if (base.Parent != null)
            if (base.Parent.Font.Equals(this.MyOtherFont))
                return false;

        if (this.MyOtherFont == null)
            return false;

        return true;
    }

    private void ResetMyOtherFont()
    {
        if (base.Parent != null)
            this.MyOtherFont = base.Parent.Font;
        else
            this.MyOtherFont = Control.DefaultFont;
    }
}

这还不够,您还必须给出一种方法来重置属性,使其再次处于环境中。ResetMyOtherFont()方法。并告诉设计师,[AmbientValue]属性。@HansPassant我有ResetMyOtherFont方法。但不确定该属性应如何在该属性上使用。有什么见解吗?当您第一次获得父字体时,您可能希望将继承的字体设置为基本字体,这样您就不会在每次访问时都有两条if语句来检查它