当使用带有水平策略 Never 的 Gtk ScrolledWindow 时,如果 child 的水平尺寸请求发生变化,我希望滚动窗口能够适应。
但情况并非如此,例如在使用扩展器时:当我扩展扩展器时,子窗口比滚动窗口更宽,滚动窗口不适应它的大小。
最小示例:
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;
}
}
}
最佳答案
尝试为您的滚动条手动创建视口(viewport),并在发送来自 child 的 SizeRequested 事件时调整来自视口(viewport)的宽度请求。
对于您的最小示例,它应该如下所示:
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;
}
}
}
关于c# - 滚动窗口中的 Gtk Expander : The width of the scrolledwindow doesn't update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16250007/