如何更新ListView的选定项目? [英] How to update ListView's selected item?
问题描述
我有一个ListView,它显示ListViewItems的多行.用户可以通过单击编辑"后打开的对话框来编辑这些项目之一.对话框关闭时,我想修改所选的ListViewItem,以使其反映新的设置.
I have a ListView which displays multiple rows of ListViewItems. The user is able to edit one of these items through a dialog which opens after clicking 'Edit.' When the dialog closes I would like to modify the selected ListViewItem such that it reflects the new settings.
这是我当前更新商品的方式:
Here is how I currently update my item:
private void btnEditSnmpV3Setting_Click(object sender, EventArgs e)
{
if (lstVwSNMPv3Settings.SelectedItems.Count > 0)
{
ListViewItem selectedItem = lstVwSNMPv3Settings.SelectedItems[0];
NetworkDiscoverySnmpSetting settings = (NetworkDiscoverySnmpSetting)selectedItem.Tag;
NetworkDiscoverySnmpV3SettingsDialog dialog = new NetworkDiscoverySnmpV3SettingsDialog(settings);
//Pass in the owner for centering of dialog.
if (dialog.ShowDialog(this) == DialogResult.OK)
{
selectedItem.SubItems.Clear();
selectedItem.Text = settings.SnmpV3Username;
selectedItem.SubItems.Add(settings.SecurityMode.ToString());
selectedItem.SubItems.Add(settings.AuthenticationProtocol.ToString());
selectedItem.SubItems.Add(settings.PrivacyProtocol.ToString());
selectedItem.Tag = settings;
}
}
}
我发现这是一个较差的解决方案,因为如果ListView的列数发生变化,我需要在多个位置触摸代码.
I found this to be a poor solution due to the fact that I need to touch code in multiple places if my ListView's number of columns changes.
我通过给NetworkDiscoverySnmpSetting
一个实用程序方法来处理了'Add'事件(而不是'Edit')中的代码重用问题:
I handled this code-reuse issue during the 'Add' event (as opposed to 'Edit') by giving NetworkDiscoverySnmpSetting
a utility method:
public ListViewItem ToListViewItem()
{
ListViewItem listViewItem = new ListViewItem();
listViewItem.Text = SnmpV3Username;
listViewItem.SubItems.Add(SecurityMode.ToString());
listViewItem.SubItems.Add(AuthenticationProtocol.ToString());
listViewItem.SubItems.Add(PrivacyProtocol.ToString());
listViewItem.Tag = this;
return listViewItem;
}
其用法如下:
private void btnAddSnmpV3Setting_Click(object sender, EventArgs e)
{
NetworkDiscoverySnmpSetting settings = new NetworkDiscoverySnmpSetting(NetworkDiscovery.ID);
NetworkDiscoverySnmpV3SettingsDialog dialog = new NetworkDiscoverySnmpV3SettingsDialog(settings);
//Pass in the owner for centering of dialog.
if (dialog.ShowDialog(this) == DialogResult.OK)
lstVwSNMPv3Settings.Items.Add(settings.ToListViewItem());
}
不幸的是,ListView.SelectedItems不允许进行集合修改.因此,它不会编译:
Unfortunately, ListView.SelectedItems does not allow collection-modification. As such, this does not compile:
lstVwSNMPv3Settings.SelectedItems[0] = settings.ToListViewItem();
我应该如何更改第一个代码段,以便在ListView的列更改时无需在多个位置更新代码?
How should I change my first code-snippet so that I do not need to update my code in multiple places when ListView's columns change?
推荐答案
您可以修改元素本身,而不必用另一个元素替换它,因为ListViewItem是一个类,因此它是一种引用类型.
You can modify the element itself rather than replacing it with another one, because ListViewItem is a class, so it's a reference type.
为此,请按照以下步骤操作:
In order to do this follow these steps:
- 获取当前选定的项目并将其保存到变量中,如下所示:
ListViewItem selectedItem = lstVwSNMPv3Settings.SelectedItems[0];
-
将您的
ToListViewItem
方法修改为void ToListViewItem(ListViewItem listViewItem)
(返回void并以ListViewItem
对象作为参数并对其进行修改,而不是创建新对象.它还应该修改现有子项目的属性,而不是创建新子项目. .它看起来或多或少是这样的:
- get currently selected item and save it to variable like this:
ListViewItem selectedItem = lstVwSNMPv3Settings.SelectedItems[0];
modify your
ToListViewItem
method tovoid ToListViewItem(ListViewItem listViewItem)
(return void and takeListViewItem
object as parameter and modify it instead of creating a new object. It should also rather modify properties of existing subitems than creating new ones. It can look more or less like this:
public void ToListViewItem(ListViewItem listViewItem)
{
listViewItem.Text = SnmpV3Username;
listViewItem.SubItems[0].Text = SecurityMode.ToString();
listViewItem.SubItems[1].Text = AuthenticationProtocol.ToString();
listViewItem.SubItems[2].Text = PrivacyProtocol.ToString();
listViewItem.Tag = this;
}
致电ToListViewItem(selectedItem);
我做了一个快速测试,该方法似乎可以毫无问题地修改现有项目的文本.
I did a quick test and the method seems to modify texts of existing items without issues.
这篇关于如何更新ListView的选定项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!