In this article, we will see how to use ListView Control in WPF.
The ListView Control use in WPF. We will use the ListView Control with Value Converter in the WPF with VB.NET. The WPF application has a ListView in a WPF Window. We will use ListView, ListView.View, GridView and GridViewColumn in the ListView Control. We will add any page in this control. We will add DrinkProduct Class, Application.xaml, Window.xaml and IntegerToBrushConverter Class.
This code of window.xaml:-This code use for window contains a ListView and we will use ListView, ListView.View, GridView and GridViewColumn.
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF ListView" Height="300" Width="300">
<Grid>
<ListView Name="ProductsListView" ItemsSource="{Binding}" Margin="5" >
<ListView.View>
<GridView>
<GridViewColumn HeaderTemplate="{StaticResource IDColHeader}"
CellTemplate="{StaticResource IDCellTemplate}">
</GridViewColumn> <GridViewColumn HeaderTemplate="{StaticResource NameColHeader}" CellTemplate="{StaticResource NameCellTemplate}">
</GridViewColumn>
<GridViewColumn HeaderTemplate="{StaticResource
PackageColHeader}" CellTemplate="{StaticResource PackCellTemplate}">
</GridViewColumn>
<GridViewColumn HeaderTemplate="{StaticResource QtyColHeader}"
CellTemplate="{StaticResource QtyCellTemplate}">
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
This code of window.xaml.vb:-This code sets the DataContext to that list of DrinkProducts.
Class Window1
Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Me.DataContext = DrinkProduct.StockCheck
End Sub
End Class
This code of Application.xaml:- The Application.xaml is set the brush value and set the style of the brush. We will use style, LinearGrandientBrush and DataTemplate.
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<LinearGradientBrush x:Key="SubtleBlue" EndPoint="0.476,-0.09" StartPoint="0.476,1.363">
<GradientStop Color="#FF7A8EEC" Offset="0.013"/>
<GradientStop Color="#FF6B93CC" Offset="1"/>
<GradientStop Color="#FFF7F6F7" Offset="0.54"/>
<GradientStop Color="#FFEDEEF4" Offset="0.46"/>
</LinearGradientBrush>
<Style x:Key="ColHeaderText" TargetType="TextBlock">
<Setter Property="FontSize" Value="14" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Background" Value="{StaticResource SubtleBlue}" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="Padding" Value="2" />
</Style>
<Style x:Key="BlueBorder" TargetType="Border">
<Setter Property="BorderBrush" Value="#FF6B93CC" />
<Setter Property="BorderThickness" Value="4"/>
<Setter Property="CornerRadius" Value="2"/>
<Setter Property="Padding" Value="2"/>
</Style>
<DataTemplate x:Key="IDColHeader">
<Border Style="{StaticResource BlueBorder}">
<TextBlock Text="Product ID " Style="{StaticResource ColHeaderText}"/>
</Border>
</DataTemplate>
<DataTemplate x:Key="NameColHeader">
<Border Style="{StaticResource BlueBorder}">
<TextBlock Text="Product Name " Style="{StaticResource
ColHeaderText}" />
</Border>
</DataTemplate>
<DataTemplate x:Key="PackageColHeader">
<Border Style="{StaticResource BlueBorder}">
<TextBlock Text="Pack Size " Style="{StaticResource ColHeaderText}"
/>
</Border>
</DataTemplate>
<DataTemplate x:Key="QtyColHeader">
<Border Style="{StaticResource BlueBorder}">
<TextBlock Text="Quantity " Style="{StaticResource ColHeaderText}"
/>
</Border>
</DataTemplate>
<DataTemplate x:Key="IDCellTemplate">
<TextBlock Foreground="MediumBlue" FontFamily="Calibri" Text="{Binding
Path=ProductID}" />
</DataTemplate>
<DataTemplate x:Key="NameCellTemplate">
<TextBlock Foreground="DarkBlue" FontWeight="Bold" Text="{Binding
Path=ProductName}" />
</DataTemplate>
<DataTemplate x:Key="PackCellTemplate">
<TextBlock Foreground="MediumBlue" Text="{Binding Path=PackageType}" />
</DataTemplate>
<local:IntegerToBrushConverter x:Key="QtyConverter" />
<DataTemplate x:Key="QtyCellTemplate">
<TextBlock
Foreground="{Binding Path=Quantity, Converter={StaticResource
QtyConverter}}"
Text="{Binding Path=Quantity}" />
</DataTemplate>
</Application.Resources>
</Application>
This code of IntegerToBrushConverter Class:- The IntegerToBrushConverter Class is use for convert the value. The Convert method in this class does all the work.
Public Class IntegerToBrushConverter
Implements IValueConverter
----------------------------------------------------------------------------------
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
If targetType IsNot GetType(Brush) Then
Return Nothing
End If
Dim Result As Integer = Integer.Parse(value.ToString())
Return (If(Result < 200, Brushes.Red, Brushes.MediumBlue))
End Function
----------------------------------------------------------------------------------
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
This code of DrinkProduct Class:- The DrinkProduct Class is a simple class. This class is use for deign the page.
Public Class DrinkProduct
Enum MaterialType
Granules
Leaf
Liquid
Paste
Powder
Other
End Enum
Sub New(ByVal ID As String, ByVal Name As String, ByVal PackType As String,ByVal BaseMaterial As MaterialType, ByVal Sales As Integer, ByVal Qty As Integer)
Me.ProductID = ID
Me.ProductName = Name
Me.PackageType = PackType
Me.Material = Material
Me.AnnualSales = Sales
Me.Quantity = Qty
End Sub
Private _ProductID As String
Public Property ProductID() As String
Get
Return _ProductID
End Get
Set(ByVal value As String)
_ProductID = value
End Set
End Property
Private _ProductName As String
Public Property ProductName() As String
Get
Return _ProductName
End Get
Set(ByVal value As String)
_ProductName = value
End Set
End Property
Private _PackageType As String
Public Property PackageType() As String
Get
Return _PackageType
End Get
Set(ByVal value As String)
_PackageType = value
End Set
End Property
Private _Material As MaterialType
Public Property Material() As MaterialType
Get
Return _Material
End Get
Set(ByVal value As MaterialType)
_Material = value
End Set
End Property
Private _quantity As Integer
Public Property Quantity() As Integer
Get
Return _quantity
End Get
Set(ByVal value As Integer)
_quantity = value
End Set
End Property
Private _annualsales As Integer
Public Property AnnualSales() As Integer
Get
Return _annualsales
End Get
Set(ByVal value As Integer)
_annualsales = value
End Set
End Property
Public Shared Function StockCheck() As List(Of DrinkProduct)
Dim CurrentProducts As New List(Of DrinkProduct)
With CurrentProducts
.Add(New DrinkProduct("CF1kg", "Coffee Powder", "1 Kg", MaterialType.Powder, 15684, 1276))
.Add(New DrinkProduct("CFB500", "Ground Coffee", "500 g", MaterialType.Powder, 22785, 12856))
.Add(New DrinkProduct("CFG500", "Coffee Granules", "500 g",
MaterialType.Granules, 19233, 5907))
.Add(New DrinkProduct("Te500", "Tea", "500 g", MaterialType.Leaf, 8544, 235))
.Add(New DrinkProduct("TeInst500", "Instant Tea", "500 g", MaterialType.Powder, 1009, 22))
.Add(New DrinkProduct("SMlk1lt", "Skimmed Milk", "1 Litre", MaterialType.Liquid, 28012, 2650))
.Add(New DrinkProduct("HiJ300", "HiJuice Drink Mix", "300 g", MaterialType.Other, 17523, 179))
.Add(New DrinkProduct("Sm400", "Smoothie", "400ml", MaterialType.Paste, 9346, 3284))
.Add(New DrinkProduct("Beef300", "Beef Drink", "300 g", MaterialType.Granules, 8316, 1965))
.Add(New DrinkProduct("Beef750", "Beef Drink", "750 g", MaterialType.Granules, 7612, 359))
End With
Return CurrentProducts
End Function
End Class
I hope this article help you.