0

我正在尝试开发一个非常简单的带有单声道和 GTK 锐利的 GUI。我有几个两个组合框,我需要根据第一个中的选定项目更改 secod 中可用的选项:

//Create window container
var mainContainer = new Table (3, 1, false);

//add form
var formContainer = new Table (1, 3, true);

//FIRST COMBOBOX
pdfSelect = new ComboBox(new string[] {"Monthly", "Monthly Offline", "Quarterly" }) { Active = 2 };
pdfSelect.Changed += new EventHandler(OnPdfSelectChanged);
formContainer.Attach (pdfSelect, 1, 2, 0, 1);

//MONTH COMBOBOX
monthSelect = new ComboBox(new string[] { "January","February","March","April","May","June","July","August","September","October","November","December" }){ Active = 0 };

formContainer.Attach(monthSelect, 2, 3, 0, 1);

mainContainer.Attach(formContainer, 0, 1, 0, 1);

win.Add(mainContainer);

我的更改事件如下:

public static void OnPdfSelectChanged(object o, EventArgs args) {
    ComboBox combo = o as ComboBox;
    if (o == null) return;
    if (combo.ActiveText.Equals("Monthly") || combo.ActiveText.Equals("Monthly Offline"))
    {
        monthSelect = new ComboBox(new string[] { "January","February","March","April","May","June","July",
            "August","September","October","November","December" }) { Active = 0 };
    }
    else
    {
        monthSelect = new ComboBox(new string[] { "January", "April", "July", "October" }) { Active = 0 };
    }
    //TODO REFRESH WINDOW
}

该代码有效,但第二个组合框似乎没有更新。我想我需要刷新窗口?

谢谢

4

3 回答 3

1

问题是您正在为变量分配一个新Gtk.ComboBox的,monthSelect但这不会自动从窗口中删除旧的组合框并添加这个新的组合框。您想要的是清除组合框的内容并添加新内容。就像是:

monthSelect.Clear();
monthSelect.AppendText("January");
monthSelect.AppendText("April");
monthSelect.AppendText("July");
monthSelect.AppendText("October");
于 2013-02-14T09:28:06.223 回答
1

我让它工作,这两个意思是Clear()像雾所说的那样调用(+1),但之后你需要分配一个单元格渲染文本,否则新项目将不会显示:

Gtk.ComboBox.Clear 方法“取消设置此组合框的所有渲染器上的所有映射。”

public static void OnPdfSelectChanged(object o, EventArgs args) {
    ComboBox combo = o as ComboBox;
    if (o == null) return;
    //Set entries
    var entries = new string[] {  "January","February","March","April","May","June","July", "August","September","October","November","December" };
    if (combo.ActiveText.Equals("Quarterly"))
    {
        entries = new string[] { "January", "April", "July", "October" };
    }
    //clear previous data
    monthSelect.Clear();
    //cells data
    ListStore store = new ListStore(typeof(string));
    foreach (var item in entries) {
        store.AppendValues(item);
    }
    //assign data to combobox
    monthSelect.Model = store;
    //renderer for cells
    var cellRenderer = new CellRendererText();
    monthSelect.PackStart(cellRenderer, true);
    monthSelect.AddAttribute(cellRenderer, "text", 0);
    //set first item as active
    monthSelect.Active = 0;
}
于 2013-02-14T11:33:09.673 回答
0

也许为时已晚,但...

((ListStore)combobox.Model).Clear(); 
combobox.AppendText("item");
于 2013-07-25T10:10:35.090 回答