Items Controls
Items Controls
1. All items controls derive from the abstract ItemsControl class, which, like ContentControl, is
a direct subclass of Control.
2. ItemsControl stores its content in an Items property (of type ItemCollection). Each
item can be an arbitrary object that by default gets rendered just as it would inside a
content control. In other words, any UIElement is rendered as expected, and
(ignoring data templates) any other type is rendered as a TextBlock containing the
string returned by its ToString method.
3. Wereas those chapters always added ListBoxItems to the Items collection, the
following example adds arbitrary objects to Items:
4. Common Functionality
Besides Items and ItemsSource, ItemsControl has a few additional interesting
properties, including the following:
- Items : an arbitrary collection off controls
ItemsControl: a control that holds a collection
. HasItems—A read-only Boolean property that makes it easy to act on the control’s
empty state from declarative XAML. From C#, you can either use this property or
simply check the value of Items.Count.
. IsGrouping—Another read-only Boolean property that tells if the control’s items are
divided into top-level groups. This grouping is done directly within the ItemsCollection
class, which contains several properties for managing and naming groups of items.
You’ll learn more about grouping in Chapter 13.
. AlternationCount and AlternationIndex—This pair of properties makes it easy to vary the
style of items based on their index. For example, an AlternationCount of 2
can be used to give even-indexed items one style and odd-indexed items another
style. Chapter 14, “Styles, Templates, Skins, and Themes,” shows an example of
using these properties.
. DisplayMemberPath—A string property that can be set to the name of a property on
each item (or a more complicated expression) that changes how each object is
rendered.
. ItemsPanel—A property that can be used to customize how the control’s items are
arranged without replacing the entire control template.
DisplayMemberPath
Figure 10.2 demonstrates what happens when DisplayMemberPath is applied to the
preceding ListBox, as follows:
<ListBox xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:sys=”clr-
namespace:System;assembly=mscorlib” DisplayMemberPath=”DayOfWeek”>
<Button>Button</Button>
<Expander Header=”Expander”/> <sys:DateTime>1/1/2012</sys:DateTime>
<sys:DateTime>1/2/2012</sys:DateTime> <sys:DateTime>1/3/2012</sys:DateTime>
</ListBox>
_
5. ItemsPanel
This mini-template, called an items panel, enables you to swap out the panel used to
arrange items while leaving everything else about the control intact.
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
… </ListBox>
<ListBox Name="lbTodoList" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}" />
<ProgressBar
Grid.Column="1"
Maximum="100"
Minimum="0"
Value="{Binding Completion}"
/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
lbTodoList.ItemsSource = items;
}
}
6. The ViewModel
dataGrid.ItemsSource = new Record[]
{
new Record { FirstName="Adam", LastName="Nathan", Website=new
Uri("http://adamnathan.net"), Gender=Gender.Male },
new Record { FirstName="Bill", LastName="Gates", Website=new
Uri("http://twitter.com/billgates"), Gender=Gender.Male, IsBillionaire=true }
};
7. The View
<DataGrid Name="dataGrid"/>
8. The output