Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
ItemsControl has a settable ItemsSource property that allows you to add data to an ItemsControl. The items in an ItemsControl are of type ItemCollection. This example demonstrates how to create an ObservableCollection<T> named Colors that is added to a ListBox.
Example
Public Class myColors
Inherits ObservableCollection(Of String)
Public Sub New()
Add("LightBlue")
Add("Pink")
Add("Red")
Add("Purple")
Add("Blue")
Add("Green")
End Sub
End Class
public class myColors : ObservableCollection<string>
{
public myColors()
{
Add("LightBlue");
Add("Pink");
Add("Red");
Add("Purple");
Add("Blue");
Add("Green");
}
}
Once you have a collection, you can bind the collection to an ItemsControl such as a ListBox. The following example shows how to create a collection to add to the list box by creating an ObjectDataProvider then binding it to the ListBox by using the ItemsSource property.
<Canvas.Resources>
<src:myColors x:Key="Colors"/>
</Canvas.Resources>
<ListBox Name="myListBox" HorizontalAlignment="Left" SelectionMode="Extended"
Width="265" Height="55" Background="HoneyDew" SelectionChanged="myListBox_SelectionChanged"
ItemsSource="{Binding Source={StaticResource Colors}}" IsSynchronizedWithCurrentItem="true">
</ListBox>
For the complete sample, see ListBox Sample.