IEditableCollectionViewAddNewItem Interface
Defines methods and properties that a CollectionView implements to enable specifying adding items of a specific type.
Assembly: WindowsBase (in WindowsBase.dll)
| Name | Description | |
|---|---|---|
![]() | CanAddNew | Gets a value that indicates whether a new item can be added to the collection.(Inherited from IEditableCollectionView.) |
![]() | CanAddNewItem | Gets a value that indicates whether a specified object can be added to the collection. |
![]() | CanCancelEdit | Gets a value that indicates whether the collection view can discard pending changes and restore the original values of an edited object.(Inherited from IEditableCollectionView.) |
![]() | CanRemove | Gets a value that indicates whether an item can be removed from the collection.(Inherited from IEditableCollectionView.) |
![]() | CurrentAddItem | Gets the item that is being added during the current add transaction.(Inherited from IEditableCollectionView.) |
![]() | CurrentEditItem | Gets the item in the collection that is being edited.(Inherited from IEditableCollectionView.) |
![]() | IsAddingNew | Gets a value that indicates whether an add transaction is in progress.(Inherited from IEditableCollectionView.) |
![]() | IsEditingItem | Gets a value that indicates whether an edit transaction is in progress.(Inherited from IEditableCollectionView.) |
![]() | NewItemPlaceholderPosition | Gets or sets the position of the new item placeholder in the collection view.(Inherited from IEditableCollectionView.) |
| Name | Description | |
|---|---|---|
![]() | AddNew() | Adds a new item to the collection.(Inherited from IEditableCollectionView.) |
![]() | AddNewItem(Object) | Adds the specified object to the collection. |
![]() | CancelEdit() | Ends the edit transaction and, if possible, restores the original value to the item.(Inherited from IEditableCollectionView.) |
![]() | CancelNew() | Ends the add transaction and discards the pending new item.(Inherited from IEditableCollectionView.) |
![]() | CommitEdit() | Ends the edit transaction and saves the pending changes.(Inherited from IEditableCollectionView.) |
![]() | CommitNew() | Ends the add transaction and saves the pending new item.(Inherited from IEditableCollectionView.) |
![]() | EditItem(Object) | Begins an edit transaction of the specified item.(Inherited from IEditableCollectionView.) |
![]() | Remove(Object) | Removes the specified item from the collection.(Inherited from IEditableCollectionView.) |
![]() | RemoveAt(Int32) | Removes the item at the specified position from the collection.(Inherited from IEditableCollectionView.) |
The IEditableCollectionViewAddNewItem interface enables application developers to specify what type of object to add to a collection. This interface extends IEditableCollectionView, so you can add, edit, and remove items in a collection. IEditableCollectionViewAddNewItem adds the AddNewItem method, which takes an object that is added to the collection. This method is useful when the collection and objects that you want to add have one or more of the following characteristics:
The objects in the CollectionView are different types.
The objects do not have a default constructor.
The object already exists.
You want to add a null object to the collection.
The following example enables a user to add various types of items to a collection. The user can enter a new item and submit the entry or cancel the transaction. The example gets an IEditableCollectionViewAddNewItem from the Items property of a ListBox and creates an object, whose type is determined by the user. Then the example calls the AddNewItem method to add the object to the collection.
The following example creates the user interface for the previous example.
<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>
The following example creates the AddItemWindow in which the user adds data for a new item.
<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>
The following example shows the logic for the AddItemWindow.
The following example shows the data types and collection that is used in the previous examples.
Available since 4.0

