0% found this document useful (0 votes)
33 views4 pages

Items Controls

ItemsControl is an abstract base class that provides functionality for controls that display a collection of items. It allows adding arbitrary objects to its Items collection to be rendered. Common properties like Items, ItemsSource, DisplayMemberPath, and ItemsPanel allow customizing how the items are displayed. DataGrid is a control optimized for displaying tabular data bound to data sources like databases. It allows sorting, editing, and other operations on multicolumn rows of data.

Uploaded by

yuvalp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Items Controls

ItemsControl is an abstract base class that provides functionality for controls that display a collection of items. It allows adding arbitrary objects to its Items collection to be rendered. Common properties like Items, ItemsSource, DisplayMemberPath, and ItemsPanel allow customizing how the items are displayed. DataGrid is a control optimized for displaying tabular data bound to data sources like databases. It allows sorting, editing, and other operations on multicolumn rows of data.

Uploaded by

yuvalp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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:

<ListBox xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/ ➥presentation”


xmlns:sys=”clr-namespace:System;assembly=mscorlib”>
<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>

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>
_

Setting DisplayMemberPath to DayOfWeek tells WPF to render the value of each


item’s DayOfWeek property rather than each item itself

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>

The code behind;

public partial class MainWindow : Window


{
public MainWindow()
{
InitializeComponent();

List<TodoItem> items = new List<TodoItem>();


items.Add(new TodoItem() { Title = "Complete this WPF tutorial", Completion = 45 });
items.Add(new TodoItem() { Title = "Learn C#", Completion = 80 });
items.Add(new TodoItem() { Title = "Wash the car", Completion = 0 });

lbTodoList.ItemsSource = items;
}
}

public class TodoItem


{
public string Title
{
get; set;
}

public int Completion


{
get; set;
}
}
Data Grid – like SmartTable
1. DataGrid is a versatile control for displaying multicolumn rows of data that can be
sorted, edited, and much more. It is optimized for easy hook-up to an in-memory
database table (such as System.Data.DataTable in ADO.NET). Wizards in Visual Studio and
technologies such as LINQ to SQL make this connection especially easy.
2. The model
public class Record
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Uri Website { get; set; }
public bool IsBillionaire { get; set; }
public Gender Gender { get; set; }
}

public enum Gender


{
Male,
Female
}

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

You might also like