3

当使用带有水平策略 Never 的 Gtk ScrolledWindow 时,如果子级的水平大小请求发生变化,我希望滚动窗口能够适应。

但情况并非如此,例如在使用扩展器时:当我扩展扩展器时,子窗口比滚动窗口更宽,滚动窗口不会适应它的大小。

最小示例:

using System;
using Gtk;

namespace ExpanderIssue
{
    class MainClass
    {
        static bool useExpander = true;

        private static Widget createScrolledWindow(Widget child)
        {
            ScrolledWindow scrolledWindow = new ScrolledWindow();
            scrolledWindow.SetPolicy(PolicyType.Never, PolicyType.Automatic);

            scrolledWindow.AddWithViewport(child);

            return scrolledWindow;
        }

        private static Widget createSingleExpaner(int index)
        {
            Button button = new Button(String.Format("ExampleButton {0}", index));
            button.WidthRequest = 800;

            if(useExpander) {
                Expander expander = new Expander(String.Format("Expander {0}", index));

                expander.Add(button);

                return expander;
            } else {
                return button;
            }
        }

        public static void Main(string[] args)
        {
            Application.Init();

            createMainWindow();

            Application.Run();
        }

        private static Window createMainWindow()
        {
            VBox vbox = new VBox();
            vbox.Spacing = 4;
            for(int i=0; i<32; ++i)
                vbox.PackStart(createSingleExpaner(i), false, false, 0);

            Window mainWindow = new Window("Expander Width Request issue");
            mainWindow.SetDefaultSize(240, 160);
            mainWindow.Add(createScrolledWindow(vbox));
            mainWindow.ShowAll();
            mainWindow.Destroyed += (sender, e) => Application.Quit();

            return mainWindow;
        }
    }
}
4

1 回答 1

2

尝试手动为滚动条创建视口,并在发送来自子级的 SizeRequested 事件时从视口调整宽度请求。

对于您的最小示例,它应该如下所示:

using System;
using Gtk;

namespace ExpanderIssue
{
    class MainClass
    {
        static bool useExpander = true;

        private static Widget createScrolledWindow(Widget child)
        {
            ScrolledWindow scrolledWindow = new ScrolledWindow();
            scrolledWindow.SetPolicy(PolicyType.Never, PolicyType.Automatic);

            scrolledWindow.Add(doWorkaround(child));

            return scrolledWindow;
        }

        private static Viewport doWorkaround(Widget child)
        {
            Viewport viewport = new Viewport();

            viewport.Add(child);

            child.SizeRequested += (o, args) =>
            {
                viewport.WidthRequest = viewport.Child.Requisition.Width;
            };

            return viewport;
        }


        private static Widget createSingleExpaner(int index)
        {
            Button button = new Button(String.Format("ExampleButton {0}", index));
            button.WidthRequest = 800;

            if(useExpander) {
                Expander expander = new Expander(String.Format("Expander {0}", index));

                expander.Add(button);

                return expander;
            } else {
                return button;
            }
        }

        public static void Main(string[] args)
        {
            Application.Init();

            createMainWindow();

            Application.Run();
        }

        private static Window createMainWindow()
        {
            VBox vbox = new VBox();
            vbox.Spacing = 4;
            for(int i=0; i<32; ++i)
                vbox.PackStart(createSingleExpaner(i), false, false, 0);

            Window mainWindow = new Window("Expander Width Request issue");
            mainWindow.SetDefaultSize(240, 160);
            mainWindow.Add(createScrolledWindow(vbox));
            mainWindow.ShowAll();
            mainWindow.Destroyed += (sender, e) => Application.Quit();

            return mainWindow;
        }
    }
}
于 2013-04-27T08:51:48.917 回答