【问题标题】:How I can show I list<string> or alternative in ComboBox ? (c#)如何在 ComboBox 中显示 I list<string> 或替代项? (C#)
【发布时间】:2020-12-10 12:29:31
【问题描述】:

其实我在我的学校有一个 c# 的工作,我需要显示一个 ComboBox 列表,但我不喜欢显示代码:

combobox.Items.Add("example")
combobox.Items.Add("example2")
combobox.Items.Add("example3")

它很长,我尝试使用 List 或 string[] 进行优化,但我不知道如何制作。

你能帮帮我吗?

【问题讨论】:

  • 您使用的是 WinForms 还是 WPF 或其他什么?
  • 我在学校使用 WPF。

标签: c# wpf list combobox


【解决方案1】:

只需将 ComboBox 的 ItemsSource 属性设置为实现 IEnumerable 接口的任何内容:

combobox.ItemsSource = new string[] { "example1", "example2", "example3" };

有关详细信息,请参阅文档:ItemsControl.ItemsSource Property

【讨论】:

    猜你喜欢
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多