c# - 不懂 ComboBox 文本渲染
问题描述
我正在尝试使用 Gtk 编写 c# 应用程序,它可以通过反射加载类并查看表单上的所有方法。我添加了一个 ComboBox 组件来显示和选择可供选择的类。但是当我填充组合框项目时,它们呈现不正确,正如您在屏幕上看到的那样
标题的重复次数与我在组合框中的项目一样多(如果项目数为 4,则重复 4 次)
为了填充组合框,我接下来做了:
ListStore model = new ListStore(typeof(string), typeof(Type));
foreach (var type in allImplementsOf)
{
var iter = model.AppendValues(type.Name, type);
CellRendererText c = new CellRendererText();
_classComboBox.PackStart(c, true);
_classComboBox.AddAttribute(c, "text", 0);
}
_classComboBox.Model = model;
我该如何解决?
PS对不起我的英语。
解决方案
ACellRenderer
在 的层上工作ComboBox
,而不是在单个条目的级别上工作。因此,您ComboBox
需要一个可以渲染每个条目的渲染器。每个条目文本被渲染n
时间的原因是您使用n
渲染器,每个条目都应用于每个条目。如果您只是将渲染器移出循环,它应该可以工作:
ListStore model = new ListStore(typeof(string), typeof(Type));
foreach (var type in allImplementsOf)
{
var iter = model.AppendValues(type.Name, type);
}
_classComboBox.Model = model;
CellRendererText c = new CellRendererText();
_classComboBox.PackStart(c, true);
_classComboBox.AddAttribute(c, "text", 0);
推荐阅读
- swift - CATransformation3D 转换标签未保存在图像上
- sql-server - 如何区分工作流中 SQL 任务中的变量?
- javascript - 获取被拖动元素的上/左坐标(不是指针)
- java - JavaFX App 在 Intellij 下工作但不是 jar
- javascript - JS克隆后用按钮删除div
- azure - 适用于 Apple iOS 13 的 Azure 推送通知服务失败
- fpga - 如何修复 quartus 14.1 web edition on linux 在使用几分钟后抛出的这个错误?
- serverless-framework - 文件 '../foo.ts' 不在 rootDir 下。rootDir 应包含所有源文件
- python - 由于 json.load 读取大 json 文件时出错
- python - 为什么我在 python 3 中收到“function upper(bytea)”错误?