首页 > 解决方案 > 不懂 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对不起我的英语。

标签: c#gtk

解决方案


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);

推荐阅读