다음을 통해 공유


IEditableCollectionViewAddNewItem 인터페이스

정의

특정 형식의 항목 추가를 지정할 수 있도록 CollectionView가 구현하는 메서드와 속성을 정의합니다.

public interface class IEditableCollectionViewAddNewItem : System::ComponentModel::IEditableCollectionView
public interface IEditableCollectionViewAddNewItem : System.ComponentModel.IEditableCollectionView
type IEditableCollectionViewAddNewItem = interface
    interface IEditableCollectionView
Public Interface IEditableCollectionViewAddNewItem
Implements IEditableCollectionView
파생
구현

예제

다음 예제에서는 사용자를 컬렉션에 다양 한 유형의 항목을 추가할 수 있습니다. 사용자 새 항목을 입력 하 고 항목을 제출 하거나 트랜잭션을 취소할 수 있습니다. 예제를 가져옵니다는 IEditableCollectionViewAddNewItem 에서 합니다 Items 의 속성을 ListBox 하 고 사용자가 결정 한 형식의 개체를 만듭니다. 다음 예제에서는 호출을 AddNewItem 개체 컬렉션에 추가 하는 방법입니다.

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

다음 예제에서는 이전 예제에 대 한 사용자 인터페이스를 만듭니다.

<StackPanel xmlns:src="clr-namespace:IEditableCollectionViewAddItemExample">
  <StackPanel.Resources>
    <src:LibraryCatalog x:Key="catalog"/>

    <!--Use AlternationConverter to create alternating background brushes
              to better distinguish each item. See AlternationConverter
              for more information.-->
    <AlternationConverter x:Key="BackgroundConverter">
      <SolidColorBrush>LightBlue</SolidColorBrush>
      <SolidColorBrush>LightGray</SolidColorBrush>
    </AlternationConverter>

    <!--The DataTemplate for LibraryItem, which is the base class
              of the other data types.-->
    <DataTemplate DataType="{x:Type src:LibraryItem}">
      <StackPanel>

        <TextBlock FontWeight="Bold">Item:</TextBlock>
        <TextBlock Text="{Binding Title, StringFormat={}Title: {0}}"/>
        <TextBlock>
          <TextBlock.Text>
            <MultiBinding StringFormat="{}Due Date: {0:d}    Call Number: {1}">
              <Binding Path="DueDate"/>
              <Binding Path="CallNumber"/>
            </MultiBinding>
          </TextBlock.Text>
        </TextBlock>
      </StackPanel>
    </DataTemplate>

    <!--The DataTemplate for Book.-->
    <DataTemplate DataType="{x:Type src:Book}">
      <StackPanel>

        <TextBlock FontWeight="Bold">Book:</TextBlock>
        <TextBlock Text="{Binding Title, StringFormat={}Title: {0}}"/>
        <TextBlock>
          <TextBlock.Text>
            <MultiBinding StringFormat="{}Author: {0}    Genre: {1}">
              <Binding Path="Author"/>
              <Binding Path="Genre"/>
            </MultiBinding>
          </TextBlock.Text>
        </TextBlock>
        <TextBlock>
          <TextBlock.Text>
            <MultiBinding StringFormat="{}Due Date: {0:d}    Call Number: {1}">
              <Binding Path="DueDate"/>
              <Binding Path="CallNumber"/>
            </MultiBinding>
          </TextBlock.Text>
        </TextBlock>
      </StackPanel>
    </DataTemplate>

    <!--The DataTemplate for MusicCD.-->
    <DataTemplate DataType="{x:Type src:MusicCD}">
      <StackPanel>

        <TextBlock FontWeight="Bold">Music CD:</TextBlock>
        <TextBlock Text="{Binding Title, StringFormat={}Title: {0}}"/>
        <TextBlock>
          <TextBlock.Text>
            <MultiBinding StringFormat="{}Artist: {0}    Tracks: {1}">
              <Binding Path="Artist"/>
              <Binding Path="NumberOfTracks"/>
            </MultiBinding>
          </TextBlock.Text>
        </TextBlock>
        <TextBlock>
          <TextBlock.Text>
            <MultiBinding StringFormat="{}Due Date: {0:d}    Call Number: {1}">
              <Binding Path="DueDate"/>
              <Binding Path="CallNumber"/>
            </MultiBinding>
          </TextBlock.Text>
        </TextBlock>
      </StackPanel>
    </DataTemplate>

    <!--The DataTemplate for MovieDVD-->
    <DataTemplate DataType="{x:Type src:MovieDVD}">
      <StackPanel>
        <TextBlock FontWeight="Bold">Movie DVD:</TextBlock>
        <TextBlock Text="{Binding Title, StringFormat={}Title: {0}}"/>
        <TextBlock>
          <TextBlock.Text>
            <MultiBinding StringFormat="{}Director: {0}    Genre: {1}    Length: {2}">
              <Binding Path="Director"/>
              <Binding Path="Genre"/>
              <Binding Path="Length"/>
            </MultiBinding>
          </TextBlock.Text>
        </TextBlock>
        <TextBlock>
          <TextBlock.Text>
            <MultiBinding StringFormat="{}Due Date: {0:d}    Call Number: {1}">
              <Binding Path="DueDate"/>
              <Binding Path="CallNumber"/>
            </MultiBinding>
          </TextBlock.Text>
        </TextBlock>
      </StackPanel>
    </DataTemplate>

  </StackPanel.Resources>

  <!--Bind a ListBox to a collection of LibraryItem objects. The collection 
          can hold objects any type that inherits from LibraryItem.-->
  <ListBox Name="catalogList" Height="350" AlternationCount="2" 
               ItemsSource="{StaticResource catalog}">

    <!--Use alternating background styles to better distinguish each item.
              See ItemsControl.AlternationIndex for more information.-->
    <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
        <Setter Property="Background" 
                Value="{Binding RelativeSource={RelativeSource Self},
                Path=(ItemsControl.AlternationIndex),
               Converter={StaticResource BackgroundConverter}}"/>

        <Setter Property="Margin" Value="3"/>
      </Style>
    </ListBox.ItemContainerStyle>
  </ListBox>

  <!--Enable the user to choose a type of item to add to the collection.-->
  <TextBlock>Select an item to add:</TextBlock>
  <RadioButton Name="book" >Book</RadioButton>
  <RadioButton Name="cd">Music CD</RadioButton>
  <RadioButton Name="dvd">DVD</RadioButton>
  <RadioButton>Other</RadioButton>

  <Button Margin="0,3,0,0" Click="Button_Click">Add Item</Button>
</StackPanel>

다음 예제에서는 AddItemWindow 사용자는 새 항목에 대 한 데이터를 추가 합니다.

<StackPanel Margin="10" Width="250">
  <StackPanel.Resources>

    <!--Add an event handler to select all text when
        a TextBox gets focus.-->
    <Style TargetType="TextBox">
      <EventSetter Event="GotFocus" Handler="TextBoxFocus"/>
    </Style>

    <!--Create a Template for HeaderedContentControl so the header is
        to the left of the content.-->
    <Style TargetType="HeaderedContentControl">
      <Setter Property="Margin" Value="2"/>
      <Setter Property="Focusable" Value="False"/>
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="HeaderedContentControl">
            <DockPanel LastChildFill="False">
              <ContentPresenter ContentSource="Header" DockPanel.Dock="Left" 
                                Focusable="False" VerticalAlignment="Center"/>
              <ContentPresenter ContentSource="Content" Margin="5,0,0,0" 
                                DockPanel.Dock="Right" VerticalAlignment="Center"/>
            </DockPanel>

          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

    <Style TargetType="Button">
      <Setter Property="Width" Value="100"/>
      <Setter Property="Margin" Value="10,15,15,15"/>

    </Style>

    <!--The DataTemplate that is used when the user 
        enters a new LibraryItem.-->
    <DataTemplate DataType="{x:Type src:LibraryItem}">
      <StackPanel>
        <HeaderedContentControl Header="Title">
          <TextBox Width="150" Text="{Binding Path=Title, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Due Date">
          <TextBox Width="150" Text="{Binding Path=DueDate, StringFormat=d, Mode=TwoWay}">

          </TextBox>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Call Number">
          <TextBox Width="150" Text="{Binding Path=CallNumber, Mode=TwoWay}"/>
        </HeaderedContentControl>
      </StackPanel>
    </DataTemplate>

    <!--The DataTemplate that is used when the user 
        enters a new Book.-->
    <DataTemplate DataType="{x:Type src:Book}">
      <StackPanel>
        <HeaderedContentControl Header="Title">
          <TextBox Width="150" Text="{Binding Path=Title, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Author">
          <TextBox Width="150" Text="{Binding Path=Author, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Genre">
          <TextBox Width="150" Text="{Binding Path=Genre, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Due Date">
          <TextBox Width="150" Text="{Binding Path=DueDate, StringFormat=d, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Call Number">
          <TextBox Width="150" Text="{Binding Path=CallNumber, Mode=TwoWay}"/>
        </HeaderedContentControl>
      </StackPanel>
    </DataTemplate>

    <!--The DataTemplate that is used when the user 
        enters a new MusicCD.-->
    <DataTemplate DataType="{x:Type src:MusicCD}">
      <StackPanel>
        <HeaderedContentControl Header="Title">
          <TextBox Width="150" Text="{Binding Path=Title, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Artist">
          <TextBox Width="150" Text="{Binding Path=Artist, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Tracks">
          <TextBox Width="150" Text="{Binding Path=NumberOfTracks, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Due Date">
          <TextBox Width="150" Text="{Binding Path=DueDate, StringFormat=d, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Call Number">
          <TextBox Width="150" Text="{Binding Path=CallNumber, Mode=TwoWay}"/>
        </HeaderedContentControl>
      </StackPanel>
    </DataTemplate>

    <!--The DataTemplate that is used when the user 
        enters a new MovieDVD.-->
    <DataTemplate DataType="{x:Type src:MovieDVD}">
      <StackPanel>
        <HeaderedContentControl Header="Title">
          <TextBox Width="150" Text="{Binding Path=Title, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Director">
          <TextBox Width="150" Text="{Binding Path=Director, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Length">
          <TextBox Width="150" Text="{Binding Path=Length, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Genre">
          <TextBox Width="150" Text="{Binding Path=Genre, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Due Date">
          <TextBox Width="150" Text="{Binding Path=DueDate, StringFormat=d, Mode=TwoWay}"/>
        </HeaderedContentControl>
        <HeaderedContentControl Header="Call Number">
          <TextBox Width="150" Text="{Binding Path=CallNumber, Mode=TwoWay}"/>
        </HeaderedContentControl>
      </StackPanel>
    </DataTemplate>
  </StackPanel.Resources>

  <!--One of the DataTemplates that defined above 
      is used by this ContentControl when the window is created.-->
  <ContentControl Content="{Binding}" />

  <StackPanel Orientation="Horizontal">
    <Button IsDefault="True" Click="Submit_Click">_Submit</Button>
    <Button IsCancel="True">_Cancel</Button>
  </StackPanel>
</StackPanel>

다음 예제에 대 한 논리는 AddItemWindow합니다.

using System.Windows;
using System.Windows.Controls;

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

        private void Submit_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        // Select all text when the TextBox gets focus.
        private void TextBoxFocus(object sender, RoutedEventArgs e)
        {
            TextBox tbx = sender as TextBox;

            tbx.SelectAll();
        }
    }
}
Imports System.Windows
Imports System.Windows.Controls

Partial Public Class AddItemWindow
    Inherits Window
    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub Submit_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        DialogResult = True
        Close()
    End Sub

    ' Select all text when the TextBox gets focus. 
    Private Sub TextBoxFocus(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim tbx As TextBox = TryCast(sender, TextBox)

        tbx.SelectAll()
    End Sub
End Class

다음 예제에서는 이전 예제에서 데이터 형식 및 사용 되는 컬렉션을 보여 줍니다.

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace IEditableCollectionViewAddItemExample
{
    // LibraryItem implements INotifyPropertyChanged so that the 
    // application is notified when a property changes.  It 
    // implements IEditableObject so that pending changes can be discarded.
    public class LibraryItem : INotifyPropertyChanged, IEditableObject
    {
        struct ItemData
        {
            internal string Title;
            internal string CallNumber;
            internal DateTime DueDate;
        }

        ItemData copyData;
        ItemData currentData;

        public LibraryItem(string title, string callNum, DateTime dueDate)
        {
            Title = title;
            CallNumber = callNum;
            DueDate = dueDate;
        }

        public override string ToString()
        {
            return String.Format("{0}, {1:c}, {2:D}", Title, CallNumber, DueDate);
        }

        public string Title
        {
            get { return currentData.Title; }
            set
            {
                if (currentData.Title != value)
                {
                    currentData.Title = value;
                    NotifyPropertyChanged("Title");
                }
            }
        }

        public string CallNumber
        {
            get { return currentData.CallNumber; }
            set
            {
                if (currentData.CallNumber != value)
                {
                    currentData.CallNumber = value;
                    NotifyPropertyChanged("CallNumber");
                }
            }
        }

        public DateTime DueDate
        {
            get { return currentData.DueDate; }
            set
            {
                if (value != currentData.DueDate)
                {
                    currentData.DueDate = value;
                    NotifyPropertyChanged("DueDate");
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion

        #region IEditableObject Members

        public virtual void BeginEdit()
        {
            copyData = currentData;
        }

        public virtual void CancelEdit()
        {
            currentData = copyData;
            NotifyPropertyChanged("");
        }

        public virtual void EndEdit()
        {
            copyData = new ItemData();
        }

        #endregion

    }

    public class MusicCD : LibraryItem
    {
        private struct MusicData
        {
            internal int SongNumber;
            internal string Artist;
        }

        MusicData copyData;
        MusicData currentData;

        public MusicCD(string title, string artist, int songNum, string callNum, DateTime dueDate)
            : base(title, callNum, dueDate)
        {
            currentData.SongNumber = songNum;
            currentData.Artist = artist;
        }

        public string Artist
        {
            get { return currentData.Artist; }
            set
            {
                if (value != currentData.Artist)
                {
                    currentData.Artist = value;
                    NotifyPropertyChanged("Artist");
                }
            }
        }

        public int NumberOfTracks
        {
            get { return currentData.SongNumber; }
            set
            {
                if (value != currentData.SongNumber)
                {
                    currentData.SongNumber = value;
                    NotifyPropertyChanged("NumberOfTracks");
                }
            }
        }

        public override void BeginEdit()
        {
            base.BeginEdit();
            copyData = currentData;
        }

        public override void CancelEdit()
        {
            base.CancelEdit();
            currentData = copyData;
        }

        public override void EndEdit()
        {
            base.EndEdit();
            copyData = new MusicData();
        }

        public override string ToString()
        {
            return string.Format(
                "Album: {0}\nArtist: {1}\nTracks: {2}\nDue Date: {3:d}\nCall Number: {4}",
                this.Title, this.Artist, this.NumberOfTracks, this.DueDate, this.CallNumber);
        }
    }

    public class Book : LibraryItem
    {
        private struct BookData
        {
            internal string Author;
            internal string Genre;
        }

        BookData currentData;
        BookData copyData;

        public Book(string title, string author, string genre, string callnum, DateTime dueDate)
            : base (title, callnum, dueDate)
        {
            this.Author = author;
            this.Genre = genre;
        }

        public string Author
        {
            get { return currentData.Author; }
            set
            {
                if (value != currentData.Author)
                {
                    currentData.Author = value;
                    NotifyPropertyChanged("Author");
                }
            }
        }

        public string Genre
        {
            get { return currentData.Genre; }
            set
            {
                if (value != currentData.Genre)
                {
                    currentData.Genre = value;
                    NotifyPropertyChanged("Genre");
                }
            }
        }

        public override void BeginEdit()
        {
            base.BeginEdit();
            copyData = currentData;
        }

        public override void CancelEdit()
        {
            base.CancelEdit();
            currentData = copyData;
        }

        public override void EndEdit()
        {
            base.EndEdit();
            copyData = new BookData();
        }

        public override string ToString()
        {
            return String.Format(
                "Title: {0}\nAuthor: {1}\nGenre: {2}\nDue Date: {3:d}\nCall Number: {4}",
                this.Title, this.Author, this.Genre, this.DueDate, this.CallNumber);
        }
    }

    public class MovieDVD : LibraryItem
    {
        private struct MovieData
        {
            internal TimeSpan Length;
            internal string Director;
            internal string Genre;
        }

        private MovieData currentData;
        private MovieData copyData;

        public MovieDVD(string title, string director, string genre, TimeSpan length, string callnum, DateTime dueDate)
            : base(title, callnum, dueDate)
        {
            this.Director = director;
            this.Length = length;
            this.Genre = genre;
        }

        public TimeSpan Length
        {
            get { return currentData.Length; }
            set
            {
                if (value != currentData.Length)
                {
                    currentData.Length = value;
                    NotifyPropertyChanged("Length");
                }
            }
        }

        public string Director
        {
            get { return currentData.Director; }
            set
            {
                if (value != currentData.Director)
                {
                    currentData.Director = value;
                    NotifyPropertyChanged("Director");
                }
            }
        }

        public string Genre
        {
            get { return currentData.Genre; }
            set
            {
                if (value != currentData.Genre)
                {
                    currentData.Genre = value;
                    NotifyPropertyChanged("Genre");
                }
            }
        }

        public override void BeginEdit()
        {
            base.BeginEdit();
            copyData = currentData;
        }

        public override void CancelEdit()
        {
            base.CancelEdit();
            currentData = copyData;
        }

        public override void EndEdit()
        {
            base.EndEdit();
            copyData = new MovieData();
        }

        public override string  ToString()
        {
            return String.Format("Title: {0}\nDirector: {1}\nGenre: {2}\nLength: {3}\nDue Date: {4:d}\nCall Number: {5}",
                this.Title, this.Director, this.Genre, this.Length, this.DueDate, this.CallNumber);
        }
    }

    public class LibraryCatalog : ObservableCollection<LibraryItem>
    {
        public LibraryCatalog()
        {
            Add(new MusicCD("A Programmers Plight", "Jon Orton", 
                12, "CD.OrtPro", new DateTime(2010, 3, 24)));
    
            Add(new Book("Cooking with Thyme", "Eliot J. Graff",
                "Home Economics", "HE.GraThy", new DateTime(2010, 2, 26)));
            
            Add(new MovieDVD("Terror of the Testers", "Molly Dempsey", 
                "Horror", new TimeSpan(1, 27, 19), "DVD.DemTer",
                new DateTime(2010, 2, 1)));
            
            Add(new MusicCD("The Best of Jim Hance", "Jim Hance", 
                15, "CD.HanBes", new DateTime(2010, 1, 31)));
            
            Add(new Book("Victor and the VB Vehicle", "Tommy Hortono", 
                "YA Fiction", "YA.HorVic", new DateTime(2010, 3, 1)));
        }
    }
}
Imports System.Collections.ObjectModel
Imports System.ComponentModel

' LibraryItem implements INotifyPropertyChanged so that the 
' application is notified when a property changes. It 
' implements IEditableObject so that pending changes can be discarded. 
Public Class LibraryItem
    Implements INotifyPropertyChanged
    Implements IEditableObject
    Private Structure ItemData
        Friend Title As String
        Friend CallNumber As String
        Friend DueDate As DateTime
    End Structure

    Private copyData As ItemData
    Private currentData As ItemData

    Public Sub New(ByVal title As String, ByVal callNum As String, ByVal dueDate As DateTime)
        Me.Title = title
        Me.CallNumber = callNum
        Me.DueDate = dueDate
    End Sub

    Public Property Title() As String
        Get
            Return currentData.Title
        End Get
        Set(ByVal value As String)
            If currentData.Title <> value Then
                currentData.Title = value
                NotifyPropertyChanged("Title")
            End If
        End Set
    End Property

    Public Property CallNumber() As String
        Get
            Return currentData.CallNumber
        End Get
        Set(ByVal value As String)
            If currentData.CallNumber <> value Then
                currentData.CallNumber = value
                NotifyPropertyChanged("CallNumber")
            End If
        End Set
    End Property

    Public Property DueDate() As DateTime
        Get
            Return currentData.DueDate
        End Get
        Set(ByVal value As DateTime)
            If value <> currentData.DueDate Then
                currentData.DueDate = value
                NotifyPropertyChanged("DueDate")
            End If
        End Set
    End Property

#Region "INotifyPropertyChanged Members"

    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

    Protected Sub NotifyPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

#End Region

#Region "IEditableObject Members"

    Public Overridable Sub BeginEdit() Implements IEditableObject.BeginEdit
        copyData = currentData
    End Sub

    Public Overridable Sub CancelEdit() Implements IEditableObject.CancelEdit
        currentData = copyData

        NotifyPropertyChanged("")
    End Sub

    Public Overridable Sub EndEdit() Implements IEditableObject.EndEdit

        copyData = New ItemData()
    End Sub

#End Region

End Class


Public Class MusicCD
    Inherits LibraryItem
    Private Structure MusicData
        Friend SongNumber As Integer
        Friend Artist As String

    End Structure

    Private copyData As MusicData
    Private currentData As MusicData

    Public Sub New(ByVal title As String, ByVal artist As String,
                   ByVal songNum As Integer, ByVal callNum As String,
                   ByVal dueDate As DateTime)

        MyBase.New(title, callNum, dueDate)
        currentData.SongNumber = songNum
        currentData.Artist = artist
    End Sub

    Public Property Artist() As String
        Get
            Return currentData.Artist
        End Get
        Set(ByVal value As String)
            If value <> currentData.Artist Then
                currentData.Artist = value
                NotifyPropertyChanged("Artist")
            End If
        End Set
    End Property

    Public Property NumberOfTracks() As Integer
        Get
            Return currentData.SongNumber
        End Get
        Set(ByVal value As Integer)
            If value <> currentData.SongNumber Then
                currentData.SongNumber = value
                NotifyPropertyChanged("NumberOfTracks")
            End If
        End Set
    End Property

    Public Overloads Overrides Sub BeginEdit()
        MyBase.BeginEdit()
        copyData = currentData
    End Sub

    Public Overloads Overrides Sub CancelEdit()
        MyBase.CancelEdit()
        currentData = copyData
    End Sub

    Public Overloads Overrides Sub EndEdit()
        MyBase.EndEdit()
        copyData = New MusicData()
    End Sub

End Class

Public Class Book
    Inherits LibraryItem
    Private Structure BookData
        Friend Author As String
        Friend Genre As String
    End Structure

    Private currentData As BookData
    Private copyData As BookData

    Public Sub New(ByVal title As String, ByVal author As String, 
                   ByVal genre As String, ByVal callnum As String, 
                   ByVal dueDate As DateTime)
        MyBase.New(title, callnum, dueDate)

        Me.Author = author
        Me.Genre = genre
    End Sub

    Public Property Author() As String
        Get
            Return currentData.Author
        End Get
        Set(ByVal value As String)
            If value <> currentData.Author Then
                currentData.Author = value
                NotifyPropertyChanged("Author")
            End If
        End Set
    End Property

    Public Property Genre() As String
        Get
            Return currentData.Genre
        End Get
        Set(ByVal value As String)
            If value <> currentData.Genre Then
                currentData.Genre = value
                NotifyPropertyChanged("Genre")
            End If
        End Set
    End Property

    Public Overloads Overrides Sub BeginEdit()
        MyBase.BeginEdit()
        copyData = currentData
    End Sub

    Public Overloads Overrides Sub CancelEdit()
        MyBase.CancelEdit()
        currentData = copyData
    End Sub

    Public Overloads Overrides Sub EndEdit()
        MyBase.EndEdit()
        copyData = New BookData()
    End Sub

End Class

Public Class MovieDVD
    Inherits LibraryItem
    Private Structure MovieData
        Friend Length As TimeSpan
        Friend Director As String
        Friend Genre As String
    End Structure

    Private currentData As MovieData
    Private copyData As MovieData


    Public Sub New(ByVal title As String, ByVal director As String,
                   ByVal genre As String, ByVal length As TimeSpan,
                   ByVal callnum As String, ByVal dueDate As DateTime)

        MyBase.New(title, callnum, dueDate)
        Me.Director = director
        Me.Length = length
        Me.Genre = genre
    End Sub

    Public Property Length() As TimeSpan
        Get
            Return currentData.Length
        End Get
        Set(ByVal value As TimeSpan)
            If value <> currentData.Length Then
                currentData.Length = value
                NotifyPropertyChanged("Length")
            End If
        End Set
    End Property

    Public Property Director() As String
        Get
            Return currentData.Director
        End Get
        Set(ByVal value As String)
            If value <> currentData.Director Then
                currentData.Director = value
                NotifyPropertyChanged("Director")
            End If
        End Set
    End Property

    Public Property Genre() As String
        Get
            Return currentData.Genre
        End Get
        Set(ByVal value As String)
            If value <> currentData.Genre Then
                currentData.Genre = value
                NotifyPropertyChanged("Genre")
            End If
        End Set
    End Property

    Public Overloads Overrides Sub BeginEdit()
        MyBase.BeginEdit()
        copyData = currentData
    End Sub

    Public Overloads Overrides Sub CancelEdit()
        MyBase.CancelEdit()
        currentData = copyData
    End Sub

    Public Overloads Overrides Sub EndEdit()
        MyBase.EndEdit()
        copyData = New MovieData()
    End Sub

End Class

Public Class LibraryCatalog
    Inherits ObservableCollection(Of LibraryItem)

    Public Sub New()
        Add(New MusicCD("A Programmers Plight", "Jon Orton", 12,
                        "CD.OrtPro", New DateTime(2010, 3, 24)))
        Add(New Book("Cooking with Thyme", "Eliot J. Graff",
                        "Home Economics", "HE.GraThy", New DateTime(2010, 2, 26)))
        Add(New MovieDVD("Terror of the Testers", "Molly Dempsey", "Horror",
                         New TimeSpan(1, 27, 19), "DVD.DemTer", New DateTime(2010, 2, 1)))
        Add(New MusicCD("The Best of Jim Hance", "Jim Hance", 15,
                        "CD.HanBes", New DateTime(2010, 1, 31)))
        Add(New Book("Victor and the VB Vehicle", "Tommy Hortono",
                        "YA Fiction", "YA.HorVic", New DateTime(2010, 3, 1)))
    End Sub
End Class

설명

IEditableCollectionViewAddNewItem 인터페이스를 통해 애플리케이션 개발자가 컬렉션에 추가할 개체 유형을 지정할 수 있습니다. 이 인터페이스는 확장 IEditableCollectionView추가할 수 있도록, 편집 및 컬렉션에서 항목을 제거 합니다. IEditableCollectionViewAddNewItem 추가 된 AddNewItem 메서드는 개체를 사용 하는 컬렉션에 추가 됩니다. 이 메서드는 컬렉션 및 개체를 추가 하려면 다음 특징 중 하나 이상의 경우에 유용 합니다.

  • 개체는 CollectionView 가지 유형이 있습니다.

  • 개체에 매개 변수가 없는 생성자가 없습니다.

  • 개체가 이미 있습니다.

  • 추가 하려는 null 개체 컬렉션입니다.

속성

CanAddNew

컬렉션에 새 항목을 추가할 수 있는지 여부를 나타내는 값을 가져옵니다.

(다음에서 상속됨 IEditableCollectionView)
CanAddNewItem

컬렉션에 지정된 개체를 추가할 수 있는지 여부를 나타내는 값을 가져옵니다.

CanCancelEdit

컬렉션 뷰에서 보류 중인 변경 내용을 삭제하고 편집된 개체의 원래 값을 복원할 수 있는지 여부를 나타내는 값을 가져옵니다.

(다음에서 상속됨 IEditableCollectionView)
CanRemove

컬렉션에서 항목을 제거할 수 있는지 여부를 나타내는 값을 가져옵니다.

(다음에서 상속됨 IEditableCollectionView)
CurrentAddItem

현재 추가 트랜잭션 중에 추가되는 항목을 가져옵니다.

(다음에서 상속됨 IEditableCollectionView)
CurrentEditItem

편집되는 컬렉션의 항목을 가져옵니다.

(다음에서 상속됨 IEditableCollectionView)
IsAddingNew

추가 트랜잭션이 진행 중인지 여부를 나타내는 값을 가져옵니다.

(다음에서 상속됨 IEditableCollectionView)
IsEditingItem

편집 트랜잭션이 진행 중인지 여부를 나타내는 값을 가져옵니다.

(다음에서 상속됨 IEditableCollectionView)
NewItemPlaceholderPosition

컬렉션 뷰에서의 새 항목 자리 표시자 위치를 가져오거나 설정합니다.

(다음에서 상속됨 IEditableCollectionView)

메서드

AddNew()

컬렉션에 새 항목을 추가합니다.

(다음에서 상속됨 IEditableCollectionView)
AddNewItem(Object)

컬렉션에 지정된 개체를 추가합니다.

CancelEdit()

편집 트랜잭션을 끝내고 가능한 경우 항목의 원래 값을 복원합니다.

(다음에서 상속됨 IEditableCollectionView)
CancelNew()

추가 트랜잭션을 끝내고 보류 중인 새 항목을 삭제합니다.

(다음에서 상속됨 IEditableCollectionView)
CommitEdit()

편집 트랜잭션을 끝내고 보류 중인 변경 내용을 저장합니다.

(다음에서 상속됨 IEditableCollectionView)
CommitNew()

추가 트랜잭션을 끝내고 보류 중인 새 항목을 저장합니다.

(다음에서 상속됨 IEditableCollectionView)
EditItem(Object)

지정된 항목에 대해 편집 트랜잭션을 시작합니다.

(다음에서 상속됨 IEditableCollectionView)
Remove(Object)

컬렉션에서 지정한 항목을 제거합니다.

(다음에서 상속됨 IEditableCollectionView)
RemoveAt(Int32)

컬렉션에서 지정된 위치의 항목을 제거합니다.

(다음에서 상속됨 IEditableCollectionView)

적용 대상