c# - 在 C# 的 ListBox 中添加 ListBoxItem?

标签 c# wpf string listbox listboxitem

我知道:

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

最佳答案

试试这个:

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

listbox.Items.Add(itm);

listbox 是 ListBox 的名称。

关于c# - 在 C# 的 ListBox 中添加 ListBoxItem?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13267657/

相关文章:

c# - WPF GroupStyle 仅适用于具有超过 1 个项目的组

c - 如何修改传递给函数的二维数组

c# - 将值从子窗体转移到父窗体 c#.net

c# - 在c#中加载xaml文件,获取具有指定名称的元素,添加按钮并保存文件

c# - WPF 和 Orchestra 入门

java - 理解Java中的方法/类调用

Python 分割函数。解包错误的值太多

c# - 停止多线程 Windows 服务

c# - 团队基础服务器 2010 API

c# - Xamarin 和 native 库