How do you create your own GTK # widget with your own controls?

I am trying to create my own GTK widget by subclassing Gtk.Bin. I do not use the GUI constructor. 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 own widget to the main window, I see nothing. Other Windows controls appear, space is allocated for this custom widget. I expect to see the “MyWidget” label in my space, but nothing appears. I go through the code in the debugger and everything calls it, but it doesn't show at runtime.

Any help would be appreciated.

+3
source share
3 answers

need to redefine:

    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 ();
        }
    }
+4

, , ShowAll() , , , .

+2

Gtk + controls (inconvenient) are not displayed by default.

Try calling .Show () on the shortcut.

0
source

Source: https://habr.com/ru/post/1796343/


All Articles