Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » WPF » Use the ListView Control in WPF

Use the ListView Control in WPF

In this article, we will see how to use ListView Control in WPF.

Author Rank :
Page Views : 1574
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


HTML clipboard

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Sapna Malik
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.