The Items property can contain items such as strings, objects, or other elements. The following example shows how to use the Items property to add content to a Menu control.
Menu myMenu = new Menu();
TextBlock myTextBlock = new TextBlock();
myTextBlock.Text = "My Text";
myMenu.Items.Add(myTextBlock);
Button myButton = new Button();
myButton.Content = "My Button";
myMenu.Items.Add(myButton);
Note that the ItemCollection is a view, so you can use the view-related functionalities such as sorting, filtering, and grouping.
For example, if you have an instance of a ListBox, myListBox, you can do the following to sort the content of the ListBox. In this example, Content is the name of the property to sort by.
myListBox.Items.SortDescriptions.Add(
new SortDescription("Content", ListSortDirection.Descending));
Note that when you do this, if the control is bound to a collection directly, the default collection view is used, and the sort criteria are applied to all other controls bound to the same collection directly. The view will not be the default view if the ItemsSource property is bound to a CollectionViewSource.
If your ItemsControl is bound directly to a collection, then you can do the following to get the default view:
CollectionView myView;
...
myView = (CollectionView)CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);
Alternately, you can specify filtering, sorting, and grouping criteria in XAML or code by using a CollectionViewSource.