C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
By default it contains a GridView. It can display data in different ways: it is a container for other controls like GridView. We do not directly use it. But it helps keep programs modular.
Based on: .NET 4.5.1
Example. Let us start with this example. It uses a GridView with three GridViewColumns nested within it. A Name (AnimalList) is defined on the ListView element. On each GridViewColumn, we specify a DisplayMemberBinding.
Syntax: Please look at the syntax of the DisplayMemberBinding attributes. The Name, ID and Size are properties on the Animal class.
Note: ListView is really a container for other controls. This example is mostly about using GridView—but that too is useful.
Example markup: XAML <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> Example code: C# 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 }); } } }
In the Window_Loaded event, we access the AnimalList by its name. We add Animal objects to its Items collection with the Add method. 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.
And: 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. The argument (such as Name) is used to match a public property.
Tip: In data binding, properties are best. Often developers will try using fields, but these are not usually recognized.
Summary. A ListView is largely a container for other controls. For most programs, I recommend instead a DataGrid or a GridView without a ListView. This makes the structure of programs flatter and simpler.