ItemsControl Class
Represents a control that can be used to present a collection of elements.
- Namespace: System.Windows.Controls
- Assembly: System.Windows (in System.Windows.dll)
Syntax - in VB
Code:
'Declaration <ContentPropertyAttribute("Items", True)> _
_ Public Class ItemsControl
_ Inherits Control
Implements IGeneratorHost
'UtilizationDim instance As ItemsControl
XAML
Code:
<... ItemsControl>
oneOrMoreItems
</ ItemsControl>
In general, an ItemsControl has two roles. It can be used to present a fixed set of elements or to view a list obtained from data binding to an object. There are several controls that inherits from ItemsControl that you can use in your application, but you can also directly use an ItemsControl. For example, if you have a collection of data that you want to display as a list of items not selectable, you can use an ItemsControl to contain the data. Whether you use the ItemsControl or a child class, you can use the ItemTemplate to specify the appearance of each element. If you want to display a fixed list, complete Items with one or more child objects FrameworkElement , and let DisplayMemberPath as an empty string by default. Do not specify ItemsSource.
If you want to display data from a source object, specify ItemsSource as a reference to an object. You usually perform this operation through a declaration Binding , and the resolution mechanism of the object used as a source of binding is possibly influenced by the DataContext into force. You can set DisplayMemberPath as the name or path of private property that should appear as given repeatedly by default for the object returned. You can leave DisplayMemberPath empty string as default and specify a DataTemplate to the ItemTemplate . This approach allows you more control over the visual representation of each element and also show several properties of each object related to the data in each cell, using a layout you define in the model.
Code:
<UserControl x: Class = "TrialListBoxSnippet.Page"
xmlns = "http://schemas.microsoft.com/winfx/2010/xaml/presentation"
xmlns: x = "http://schemas.microsoft.com/winfx/2010/xaml"
xmlns: src = "clr-namespace: TrialListBoxSnippet"
>
<StackPanel x: Name = "LayoutRoot" Background = "White" Margin = "10,10,10,10">
<StackPanel Orientation = "Horizontal">
<TextBlock Margin = "5" Text = "ListBox with unbound data:" />
<ListBox Width = "150" Margin = "0,5,0,10">
<TextBlock Text = "TextBlock" />
<TextBox Text = "TextBox" />
<Button Content = "Button" />
<Rectangle Fill = "LightBlue" Height = "20" Width = "100" Margin = "2,2,2,2" />
<Ellipse Fill = "Coral" Height = "20" Width = "150" Margin = "2,2,2,2" />
</ ListBox>
<TextBlock Margin = "5" Text = "ListBox bound with data:" />
<Grid>
<Grid.Resources>
<Src: Customers x: Key = "customers" />
</ Grid.Resources>
<ListBox ItemsSource = "(StaticResource Customers)" Width = "250" Margin = "0,5,0,10"
DisplayMemberPath = "LastName" />
</ Grid>
</ StackPanel>
<StackPanel Orientation = "Horizontal">
<TextBlock Margin = "5" Text = "ListBox ItemTemplate with:" Width = "160" />
<Grid>
<Grid.Resources>
<Src: Customers x: Key = "customers" />
</ Grid.Resources>
<ListBox ItemsSource = "(StaticResource Customers)" Width = "350" Margin = "0,5,0,10">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation = "Horizontal">
<TextBlock Padding = "5,0,5,0"
Text = "(Binding FirstName)" />
<TextBlock Text = "(Binding LastName)" />
<TextBlock Text = "" />
<TextBlock Text = "(Binding) Address" />
</ StackPanel>
</ DataTemplate>
</ ListBox.ItemTemplate>
</ ListBox>
</ Grid>
<Grid>
<Grid.Resources>
<Style x: Key = "horizontalListBoxStyle" TargetType = "ListBox">
<Setter Property = "ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation = "Horizontal"
VerticalAlignment = "Center"
HorizontalAlignment = "Center" />
</ ItemsPanelTemplate>
</ Setter.Value>
</ Setter>
<Setter Property = "ItemTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation = "Horizontal">
<TextBlock Padding = "5,0,5,0"
Text = "(Binding FirstName)" />
<TextBlock Text = "(Binding LastName)" />
</ StackPanel>
</ DataTemplate>
</ Setter.Value>
</ Setter>
</ Style>
<Src: Customers x: Key = "items" />
</ Grid.Resources>
<ListBox Height = "25" Width = "420" ItemsSource = "(items) StaticResource" Style = "(StaticResource horizontalListBoxStyle)" />
</ Grid>
</ StackPanel>
<TextBlock Margin = "5" Text = "ListBox with selectionChanged event handler:" Width = "270 'HorizontalAlignment =" Left "/>
<ListBox Width = "150" Margin = "0,7,0,7" selectionChanged = "PrintText" HorizontalAlignment = "Left">
<ListBoxItem Content = "Item 1" />
<ListBoxItem Content = "Item 2" />
<ListBoxItem Content = "Item 3" />
<ListBoxItem Content = "Item 4" />
<ListBoxItem Content = "Item 5" />
</ ListBox>
<TextBlock Name = "textBlock1" />
</ StackPanel>
</ UserControl>
Bookmarks