简体   繁体   中英

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. I am not using the Stetic GUI builder. This widget will contain several standard Gtk widgets (VBoxs, Labels, Buttons, etc).

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. 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.

Try calling .Show () on your Label.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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