简体   繁体   English

如何使用自己的控件创建自定义GTK#Widget?

[英]How do you create a custom GTK# Widget with its own controls?

I am trying to create a custom GTK Widget by subclassing Gtk.Bin. 我试图通过继承Gtk.Bin来创建自定义GTK Widget。 I am not using the Stetic GUI builder. 我没有使用Stetic GUI构建器。 This widget will contain several standard Gtk widgets (VBoxs, Labels, Buttons, etc). 此小部件将包含几个标准Gtk小部件(VBox,标签,按钮等)。

public class MyWidget : Gtk.Bin
{
    public MyWidget : base ()
    {
        build ();
    }
    private void build ()
    {
        VBox vbox1 = new Vbox (true, 0);
        vbox1.PackStart (new Label ("MyWidget"), true, true, 0);
        this.Add (vbox1);
    }
}

Meanwhile when I add my custom widget to the main window, I don't see anything. 同时,当我将自定义小部件添加到主窗口时,我什么也看不到。 The windows other controls show up, space is allocated for this custom widget. 显示其他控件的窗口,为此自定义窗口小部件分配空间。 I expect to see the label "MyWidget" in its space, but nothing shows up. 我希望在它的空间看到标签“MyWidget”,但没有任何显示。 I step thru the code in the debugger and it all gets called but its a no show at runtime. 我逐步通过调试器中的代码并调用它,但它在运行时没有显示。

Any help would be appreciated. 任何帮助,将不胜感激。

need to override: 需要覆盖:

    protected override void OnSizeAllocated (Gdk.Rectangle allocation)
    {
        if (this.Child != null)
        {
            this.Child.Allocation = allocation;
        }
    }

    protected override void OnSizeRequested (ref Requisition requisition)
    {
        if (this.Child != null)
        {
            requisition = this.Child.SizeRequest ();
        }
    }

或者更有可能的是,ShowAll()是所有子项被打包后构建方法中的最后一行,除非您不希望默认情况下其中一些是可见的。

Gtk+ controls are (frustratingly) not visible by default. 默认情况下,Gtk +控件(令人沮丧)不可见。

Try calling .Show () on your Label. 尝试在Label上调用.Show()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM