user1797675
user1797675

Reputation:

Edit specific listview item in C# WPF

Is it possible to edit a specific listview item at runtime?

for (int i = 0; i < listViewServers.Items.Count; i++)
{
   if ( listViewServers.Items[i].COLUMNNAME == "My Column Name")
        {
           listViewServers.Items[i].COLUMNNAME.data = "New Cell Text";
        }
}

Upvotes: 0

Views: 778

Answers (1)

vesan
vesan

Reputation: 3369

Yes, there is a way. But should you do it? No. In WPF, you should use data binding to bind your controls to actual data, then, if you do it right, you can just change the data and the UI will update automatically. If you are not doing any fancy graphics or interactions, your code-behind (*.xaml.cs) should have no code apart from the autogenerated constructor and you should not access controls from C# code.

I suggest reading up on data binding and the MVVM pattern, for example here, here or here.

That said, you can access the ListView items, but you have to know what you're putting into the Items collection or in ItemsSource (controls via XAML? controls via code? ViewModel classes with DataTemplates?). When you know this, you can just do:

foreach (YourClass obj in ListViewServers.Items.OfType<YourClass>())
{
  //do stuff with obj
}

Upvotes: 0

Related Questions