Skip to content

Data binding of child controls of ToolStripControlHost #6297

@hdefucom

Description

@hdefucom
  • .NET Core Version:
    net40 to net6.0-windows

  • Have you experienced this same bug with .NET Framework?:

Yes

Problem description:
Add a ListBox to the ToolStripControlHost.
ListtBox cannot display data bound with datasource,
However, it can be displayed by using items.Add.
This problem only occurs in the ListBox in the ToolStripControlHost.


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            _dropDown = new ToolStripDropDown();
            _dropDown.Padding = Padding.Empty;

            box = new ListBox()
            {
                Margin = Padding.Empty,
                Dock = DockStyle.Fill,
                SelectionMode = SelectionMode.MultiSimple
            };

            //*******************************************************  Can display
            //box.Items.AddRange(Enumerable.Range(10000000, 20)
            //    .Select(i => i.ToString())
            //    .Select(i => new TestItem() { Text = i, Value = i })
            //    .OfType<object>().ToArray());
            //*************************************************************************

            //*******************************************************  Cannot display
            box.DisplayMember = "Text";
            box.ValueMember = "Value";
            box.DataSource = new ObservableCollection<TestItem>(
                Enumerable.Range(1, 10).Select(i => i.ToString())
                .Select(i => new TestItem() { Text = i, Value = i })
                );
            //*************************************************************************


            host = new ToolStripControlHost(box) { Padding = Padding.Empty, Margin = Padding.Empty };
            _dropDown.Items.Add(host);
        }

        ListBox box;
        ToolStripControlHost host;
        ToolStripDropDown _dropDown;


        private void button1_Click(object sender, EventArgs e)
        {
            _dropDown.Show(Control.MousePosition);
        }
        private class TestItem
        {
            public string Text { get; set; }
            public string Value { get; set; }

            public override string ToString()
            {
                return Text;
            }
        }
    }



Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions