ListView control in WPF
ListView:
Namespace : System.Windows.Controls.ListView
Designer Code:
<ListView Name="lst1" SelectionChanged="lst1_SelectionChanged">
<ListViewItem>One</ListViewItem>
<ListViewItem>Two</ListViewItem>
<ListViewItem>Thre</ListViewItem>
<ListViewItem>Four</ListViewItem>
</ListView>
Code Behind:
// Window1.xaml.cs
private void lst1_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
ListViewItem lstval = (ListViewItem)lst1.SelectedItem;
}
VirtualMode is supported by default when the ItemsPanel=VirtualizedItemsPanel.
Without specifying a something special in the “View” the ListView looks just like a ListBox.
for view like LargeIcon, SmallIcon, Details use the View property to specify how you want the control to layout.
Designer Code:
<ListView.View>
<GridView>
<GridViewColumn Header="Name"
DisplayMemberBinding="{Binding Path=Name}"/>
<GridViewColumn Header="Value"
DisplayMemberBinding="{Binding Path=Value}"/>
</GridView>
</ListView.View>
Code Behind:
// Data.cs
public class SampeData
{
public string Name { get; set; }
public string Value { get; set; }
}
//DataBinding
SampeData data = getDataItem(index);
this.LstView1.Items.Add(data);
Sample - Building up details view
Column headers in the ListView are specified in the ListView.View, which is set to be a GridView.
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WindowsApplication10.Window1">
<Window.Resources>
<XmlDataProvider x:Key="EmployeeData" XPath="/EmployeeData">
<EmployeeData xmlns="">
<Employee>
<FirstName>Mac</FirstName>
<LastName>Ray</LastName>
</Employee>
<Employee>
<FirstName>Agnes</FirstName>
<LastName>Ravi</LastName>
</Employee>
</EmployeeData>
</XmlDataProvider>
</Window.Resources>
<StackPanel>
<ListView ItemsSource="{Binding Source={StaticResource EmployeeData},
XPath=Employee}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,20,0,0">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name"
DisplayMemberBinding="{Binding XPath=FirstName}" />
<GridViewColumn Header="Last Name"
DisplayMemberBinding="{Binding XPath=LastName}" />
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Window>
DataBinding From Database in Listview:
//Create the ListView
ListView listview = new ListView();
//Create the GridView and create GridViewColumns
GridView gridview = new GridView();
GridViewColumn gvcolumn = new GridViewColumn();
gvcolumn.Header = "FirstName";
gvcolumn.DisplayMemberBinding = new Binding("FirstName");
gridview.Columns.Add(gvcolumn);
gvcolumn = new GridViewColumn();
gvcolumn.Header = "LastName";
gvcolumn.DisplayMemberBinding = new Binding("LastName");
gridview.Columns.Add(gvcolumn);
//Set the ListView's View to GridView
listview.View = gridview;
//Set the DataContext property of the ListView to
//the DataTable and bind the ItemsSourceProperty to {Binding}
Binding bind = new Binding();
//Use this (Or)
listview.DataContext = table;
listview.SetBinding(ListView.ItemsSourceProperty, bind);
//Set the content of the Window to the ListView
Content = listview;
//alternate code
listview.DataContext = ds.Tables[0].DefaultView;
Checkboxes in Listview:
<GridViewColumn Header="Balance" Width="140">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding}" Name="Complete"
IsThreeState="False" Checked="onButtonClick" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>