【问题标题】:adding a ListBoxItem in a ListBox in C#?在 C# 的 ListBox 中添加 ListBoxItem?
【发布时间】:2012-11-07 10:22:02
【问题描述】:

我知道:

String test = "test";
ListBox.Items.Add(test);

String test = "test";
int index = 1;
ListBox.Items.Insert(index, String);

在ListBox中添加String,但我想插入ListBoxItem,怎么做? 以前我知道

var contentToString = (String)ListBoxItem.Content;

只是将 ListBoxItem 转换为 String,但我不能做相反的事情将 String 转换为 ListBoxItem

【问题讨论】:

  • 这是asp.net还是winforms?
  • @DaniloVulović:ASP.NET ListBox 没有内容属性。
  • winforms中没有ListBoxItem!
  • 具体来说,我正在编写一个 Windows 8 Metro 应用程序。我使用 C# 和 XAML。

标签: c# wpf string listbox listboxitem


【解决方案1】:

试试这个:

ListBoxItem itm = new ListBoxItem();
itm.Content = "some text";

listbox.Items.Add(itm);

listbox 是 ListBox 的名称。

【讨论】:

    【解决方案2】:

    你可以这样做

    ListBox1.Items.Insert(0,new ListItem("ITEM 1", "Value"))
    

    【讨论】:

    • 不工作,使用 C# 和 XAML 的 Windows 8 Metro 应用程序中没有 ListItem
    【解决方案3】:

    您的对象将始终位于 ListBoxItem 中,如果您不明确添加,ListBox 将为您生成一个。要获取您使用的 ListBoxItem:

    var listboxitem = (ListBoxItem)listbox.ItemContainerGenerator.ContainerFromItem(myItem);
    

    【讨论】:

      猜你喜欢
      • 2019-04-29
      • 2011-04-03
      • 2018-01-09
      • 1970-01-01
      • 2014-02-06
      • 2019-02-09
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多