IEditableCollectionViewAddNewItem.AddNewItem(Object) メソッド

定義

指定したオブジェクトをコレクションに追加します。

public:
 System::Object ^ AddNewItem(System::Object ^ newItem);
public object AddNewItem (object newItem);
abstract member AddNewItem : obj -> obj
Public Function AddNewItem (newItem As Object) As Object

パラメーター

newItem
Object

コレクションに追加するオブジェクト。

戻り値

コレクションに追加されるオブジェクト。

次の例では、 メソッドを AddNewItem 呼び出して、 オブジェクトをコレクションに追加します。 完全な例については、 クラスを IEditableCollectionViewAddNewItem 参照してください。

using System;
using System.ComponentModel;
using System.Windows;

namespace IEditableCollectionViewAddItemExample
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            IEditableCollectionViewAddNewItem viewToAddDisparateItems =
                catalogList.Items as IEditableCollectionViewAddNewItem;

            if (!viewToAddDisparateItems.CanAddNewItem)
            {
                MessageBox.Show("You cannot add items to the list.");
                return;
            }

            // Create a window that prompts the user to enter a new
            // item to sell.
            AddItemWindow win = new AddItemWindow();

            // Create an item, depending on which RadioButton is selected.
            // Radio buttons correspond to book, cd, dvd, or other.
            LibraryItem newItem;

            if ((bool)book.IsChecked)
            {
                newItem = new Book("Enter the book title", "Enter an Author", "Enter a Genre",
                    "Enter a call number", DateTime.Now + new TimeSpan(21, 0, 0, 0));
            }
            else if ((bool)cd.IsChecked)
            {
                newItem = new MusicCD("Enter the Album", "Enter the artist", 0, "CD.******",
                    DateTime.Now + new TimeSpan(14, 0, 0, 0));
            }

            else if ((bool)dvd.IsChecked)
            {
                newItem = new MovieDVD("Enter the movie title", "Enter the director",
                    "Enter the genre", new TimeSpan(), "DVD.******",
                    DateTime.Now + new TimeSpan(7, 0, 0, 0));
            }
            else
            {
                newItem = new LibraryItem("Enter the title", "Enter the call number",
                        DateTime.Now + new TimeSpan(14, 0, 0, 0));
            }

            // Add the new item to the collection by calling
            // IEditableCollectionViewAddNewItem.AddNewItem.
            // Set the DataContext of the AddItemWindow to the
            // returned item.
            win.DataContext = viewToAddDisparateItems.AddNewItem(newItem);

            // If the user submits the new item, commit the new
            // object to the collection.  If the user cancels 
            // adding the new item, discard the new item.
            if ((bool)win.ShowDialog())
            {
                viewToAddDisparateItems.CommitNew();
            }
            else
            {
                viewToAddDisparateItems.CancelNew();
            }
        }
    }
}
Imports System.ComponentModel
Imports System.Windows

Partial Class Window1
    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)

        Dim viewToAddDisparateItems As IEditableCollectionViewAddNewItem =
            TryCast(catalogList.Items, IEditableCollectionViewAddNewItem)

        If Not viewToAddDisparateItems.CanAddNewItem Then
            MessageBox.Show("You cannot add items to the list.")
            Exit Sub
        End If

        ' Create a window that prompts the user to enter a new 
        ' item to sell. 
        Dim win As New AddItemWindow()

        ' Create an item, depending on which RadioButton is selected. 
        ' Radio buttons correspond to book, cd, dvd, or other. 
        Dim newItem As LibraryItem

        If CBool(Book.IsChecked) Then
            newItem = New Book("Enter the book title", "Enter an Author",
                "Enter a Genre", "Enter a call number",
                DateTime.Now + New TimeSpan(21, 0, 0, 0))
        ElseIf CBool(cd.IsChecked) Then
            newItem = New MusicCD("Enter the Album", "Enter the artist",
                0, "CD.******", DateTime.Now + New TimeSpan(14, 0, 0, 0))

        ElseIf CBool(dvd.IsChecked) Then
            newItem = New MovieDVD("Enter the movie title",
                "Enter the director", "Enter the genre", New TimeSpan(),
                "DVD.******", DateTime.Now + New TimeSpan(7, 0, 0, 0))
        Else
            newItem = New LibraryItem("Enter the title",
                "Enter the call number",
                DateTime.Now + New TimeSpan(14, 0, 0, 0))
        End If

        ' Add the new item to the collection by calling 
        ' IEditableCollectionViewAddNewItem.AddNewItem. 
        ' Set the DataContext of the AddItemWindow to the 
        ' returned item. 
        win.DataContext = viewToAddDisparateItems.AddNewItem(newItem)

        ' If the user submits the new item, commit the new 
        ' object to the collection. If the user cancels 
        ' adding the new item, discard the new item. 
        If CBool(win.ShowDialog()) Then
            viewToAddDisparateItems.CommitNew()
        Else
            viewToAddDisparateItems.CancelNew()
        End If
    End Sub
End Class

注釈

メソッドを AddNewItem 呼び出すと、追加トランザクションが開始されます。 または CancelNew メソッドをCommitNew呼び出して、追加トランザクションを終了する必要があります。

新しい項目は常にコレクション ビューに表示されます。 が呼び出されると、ビューに適用されるフィルター処理、並べ替え、またはグループ化が新しい項目に CommitNew 適用されます。

適用対象