Home
Map
ListView Control: Add Items to GridViewUse the WPF ListView control. The inner GridView is used to display data.
WPF
This page was last reviewed on Sep 24, 2022.
ListView. A ListView displays data. By default it contains a GridView. It can display data in different ways—it is a container for other controls like GridView.
Control notes. ListView is really a container for other controls. This example is mostly about using GridView—but that too is useful.
This example uses a GridView with 3 GridViewColumns nested within it. A Name (AnimalList) is defined on the ListView element. On each GridViewColumn, we specify a DisplayMemberBinding.
Detail Please look at the syntax of the DisplayMemberBinding attributes. The Name, ID and Size are properties on the Animal class.
<Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ListView" Height="350" Width="525" Loaded="Window_Loaded"> <Grid> <ListView HorizontalAlignment="Left" Height="299" Margin="10,10,0,0" VerticalAlignment="Top" Width="497" Name="AnimalList"> <ListView.View> <GridView> <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="100"/> <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}" Width="100"/> <GridViewColumn Header="Size" DisplayMemberBinding="{Binding Size}" Width="100"/> </GridView> </ListView.View> </ListView> </Grid> </Window>
using System.Windows; class Animal { public string Name { get; set; } public string ID { get; set; } public int Size { get; set; } } namespace WpfApplication2 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { AnimalList.Items.Add(new Animal() { Name = "Cat", ID = "123A", Size = 100 }); AnimalList.Items.Add(new Animal() { Name = "Dog", ID = "456X", Size = 200 }); AnimalList.Items.Add(new Animal() { Name = "Rabbit", ID = "333E", Size = 40 }); } } }
Some notes. In the Window_Loaded event, we access the AnimalList by its name. We add Animal objects to its Items collection with the Add method.
And The public properties on the Animal type (Name, ID, Size) match the bindings specified in the GridView.
So Where "Binding Name" is specified, the Name string is displayed in that row's cell.
Finally In this way, we populate each of the three cells of the GridView within the ListView. We operate mostly on the GridView.
DisplayMemberBinding. When creating this example, the most confusing part was using the DisplayMemberBinding. The curly brackets within the attribute are required: these are part of the XAML.
And The argument (such as Name) is used to match a public property. In data binding, properties are best.
Summary. A ListView is largely a container for other controls. For most programs, consider instead a DataGrid or a GridView without a ListView.
DataGrid
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 24, 2022 (rewrite).
Home
Changes
© 2007-2024 Sam Allen.