IEditableCollectionView.Remove(Object) 方法

定義

從集合中移除指定的項目。

public:
 void Remove(System::Object ^ item);
public void Remove(object item);
abstract member Remove : obj -> unit
Public Sub Remove (item As Object)

參數

item
Object

要移除的物品。

範例

以下範例要求 CanRemove 檢查某項目是否能從集合中移除。 如果項目可以移除,範例會提示使用者確認動作,若使用者點擊「是」則呼叫Remove。 關於整個範例,請參見 「透過使用 IEditableCollectionView 變更集合範例」。

IEditableCollectionView editableCollectionView =
        itemsControl.Items;

if (!editableCollectionView.CanRemove)
{
    _ = MessageBox.Show("You cannot remove items from the list.");
    return;
}

if (MessageBox.Show("Are you sure you want to remove " + item.Description,
                    "Remove Item", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
    editableCollectionView.Remove(itemsControl.SelectedItem);
}
Dim editableCollectionView As IEditableCollectionView = TryCast(itemsControl.Items, IEditableCollectionView)

If Not editableCollectionView.CanRemove Then
    MessageBox.Show("You cannot remove items from the list.")
    Return
End If

If MessageBox.Show("Are you sure you want to remove " & item.Description, "Remove Item", MessageBoxButton.YesNo) = MessageBoxResult.Yes Then
    editableCollectionView.Remove(itemsControl.SelectedItem)
End If

備註

如果 item 不在集合中,則 Remove 不會有任何作用。

適用於