重命名列表框中的项目

Tav*_*usi 5 c# listbox

我想重命名列表框中选定的项目。我怎样才能做到这一点?谢谢。

Luk*_*ett 5

编辑:几年后重新审视这个问题;以下是根据您使用的 UI 框架执行此操作的方法。这假设您想要更改所选文本。

ASP.Net Web 表单

protected void ChangeListBoxSelectedItemText(string textToChangeTo)
{
    lstBoxExample.SelectedItem.Text = textToChangeTo;
}
Run Code Online (Sandbox Code Playgroud)

WPF - 假设 ListBox 包含 Label 对象

// To achieve this in WPF you have to cast the object
// This is because a ListBox can contain numerous types of UI objects
var selectedLabel = (Label)lstBoxExample.SelectedItem;
selectedLabel.Content = "Text to change to";
Run Code Online (Sandbox Code Playgroud)

窗体

// There may very well be a better way to do this
lstBoxExample.Items[lstBoxExample.SelectedIndex] = "New Item";
Run Code Online (Sandbox Code Playgroud)

  • 啊,这只是表明指定您正在使用的 UI 框架是多么重要。您只是很幸运,他们恰好使用 ASP.Net,因为正如我已经说过的,WinForms 或 WPF 并非如此。 (2认同)