Reputation: 95
I use a ToolStripControlHost to wrap a ListBox control for adding it into a ToolStripDropDown, but found items I assign to ListBox.DataSource not shown up, and ComboBox.DataSource not work as well, I don't understand why ListContorl.DataSource not function in ToolStripControlHost.
ListBox listBox = new ListBox();
listBox.DataSource = new string[] { "1", "2", "3" };
ToolStripControlHost host = new ToolStripControlHost(listBox)
{
Margin = Padding.Empty,
Padding = Padding.Empty,
AutoSize = false
};
ToolStripDropDown dropDown = new ToolStripDropDown() { AutoClose = false };
dropDown.Items.Add(host);
dropDown.Show();
Edit
I found the problem is ToolStripDropDown has not parents to provide BindingContext, so it will happen to any control with DataManager.
Upvotes: 2
Views: 188
Reputation: 95
I found the problem is ToolStripDropDown has no parents to provide a BindingContext, so the solution is assign the BindingContext of the Form.
ListBox listBox = new ListBox();
listBox.DataSource = new string[] { "1", "2", "3" };
listBox.BindingContext = this.BindingContext; //assign a BindingContext
ToolStripControlHost host = new ToolStripControlHost(listBox)
{
Margin = Padding.Empty,
Padding = Padding.Empty,
AutoSize = false
};
ToolStripDropDown dropDown = new ToolStripDropDown() { AutoClose = false };
dropDown.Items.Add(host);
dropDown.Show();
Upvotes: 0
Reputation: 5266
Good question. Seems like the ListBox
has to be added to a top level control (such as a Form
) in order to force it to use the DataSource
property. E.g. Add this code after the DataSource
is assigned:
public class DataForm : Form {
ToolStripDropDown dropDown = new ToolStripDropDown() { AutoClose = true };
ListBox listBox = new ListBox();
public DataForm() {
listBox.DataSource = new string[] { "1", "2", "3" };
var hWnd = listBox.Handle; // required to force handle creation
using (var f = new Form()) {
f.Controls.Add(listBox);
f.Controls.Remove(listBox);
}
ToolStripControlHost host = new ToolStripControlHost(listBox) {
Margin = Padding.Empty,
Padding = Padding.Empty,
AutoSize = false
};
dropDown.Items.Add(host);
}
protected override void OnMouseClick(MouseEventArgs e) {
base.OnMouseClick(e);
dropDown.Show(Cursor.Position);
}
}
You could also look at the ListBox.cs
source code to try and figure out underlying cause: http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ListBox.cs,03c7f20ed985c1fc
Upvotes: 1