我正在尝试用单声道和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
}
代码有效,但第二个组合框似乎没有更新。我想我需要刷新窗口?
由于
答案 0 :(得分:1)
问题是您要为Gtk.ComboBox
变量分配一个新的monthSelect
,但不会自动从窗口中删除旧的组合框并添加新的组合框。你想要的是清除组合框的内容并添加新的内容。类似的东西:
monthSelect.Clear();
monthSelect.AppendText("January");
monthSelect.AppendText("April");
monthSelect.AppendText("July");
monthSelect.AppendText("October");
答案 1 :(得分:1)
我得到它的工作两个意思是调用Clear()
作为雾说(+ 1)但在那之后你需要分配一个单元格的rendertext或新的项目将不会显示:
Gtk.ComboBox.Clear Method "Unsets all the mappings on all renderers for this combo box."
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;
}
答案 2 :(得分:0)
也许为时已晚,但......
((ListStore)combobox.Model).Clear();
combobox.AppendText("item");