Monday, July 23, 2012

ListView Tutorial



<Window x:Class="ListViewTutorial.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Window.Resources>
        <!--ListView Styling Start-->
        <LinearGradientBrush x:Key="BackgroundBrush" StartPoint="0,0" EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#FF6FBDE8" Offset="0"/>
                    <GradientStop Color="#FF4385BE" Offset="1"/>
                </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="HighlightBackgroundBrush" StartPoint="0,0" EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#FF97d3f3" Offset="0"/>
                    <GradientStop Color="#FF4385BE" Offset="1"/>
                </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="BorderBrush" StartPoint="0,0" EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#FFAFDDF6" Offset="0"/>
                    <GradientStop Color="#FF2969AA" Offset="1"/>
                </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="PressedBorderBrush" StartPoint="0,0" EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#FF75aac7" Offset="0"/>
                    <GradientStop Color="#FF143c65" Offset="1"/>
                </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>

        <Style x:Key="GridViewColumnHeaderGripper" TargetType="Thumb">
            <Setter Property="Width" Value="20"/>
            <Setter Property="Background" Value="#2e566b"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Thumb}">
                        <Border Padding="{TemplateBinding Padding}" Background="Transparent">
                            <Rectangle HorizontalAlignment="Center" Width="1" Fill="{TemplateBinding Background}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="{x:Type GridViewColumnHeader}" TargetType="GridViewColumnHeader">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Foreground" Value="#FFFFFF" />
            <Setter Property="FontWeight" Value="DemiBold"/>

            <Setter Property="MinWidth" Value="40"/>
            <Setter Property="Height" Value="21"/>

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="GridViewColumnHeader">
                        <Grid>
                            <Border Name="HeaderBorder" Padding="{TemplateBinding Padding}" BorderThickness="0,1,0,1" BorderBrush="{StaticResource BorderBrush}" Background="{StaticResource BackgroundBrush}">
                                <ContentPresenter Name="HeaderContent" Margin="0,0,0,1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                            <Thumb x:Name="PART_HeaderGripper" HorizontalAlignment="Right" Margin="0,0,-9,0" Style="{StaticResource GridViewColumnHeaderGripper}"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="HeaderBorder" Property="Background" Value="{StaticResource HighlightBackgroundBrush}"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter TargetName="HeaderBorder" Property="Background" Value="{StaticResource PressedBorderBrush}"/>
                                <Setter TargetName="HeaderContent" Property="Margin" Value="1,1,0,0"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Role" Value="Floating">
                    <Setter Property="Opacity" Value="0.7"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="GridViewColumnHeader">
                                <Canvas Name="PART_FloatingHeaderCanvas">
                                    <Rectangle Fill="#60000000" Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight}"/>
                                </Canvas>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
                <Trigger Property="Role" Value="Padding">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="GridViewColumnHeader">
                                <Border Name="HeaderBorder" BorderThickness="0,1,0,1" BorderBrush="{StaticResource BorderBrush}" Background="{StaticResource BackgroundBrush}"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="FontSize" Value="12" />
            <Setter Property="Height" Value="18" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
        </Style>

        <Style x:Key="BorderedItem" TargetType="ListViewItem">
            <Setter Property="BorderBrush" Value="Red" />
            <Setter Property="BorderThickness" Value="2" />
        </Style>
     
        <!--ListView Styling End-->
    </Window.Resources>
    <Grid>
        <ListView Name="LstSalesData" Grid.Row="4" Grid.ColumnSpan="3"
                  ScrollViewer.VerticalScrollBarVisibility="Auto"
                  Height="300"
                  Width="500"
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                  SelectionMode="Multiple"
                  Margin="0,6,6,0"
                  VerticalAlignment="Top">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Order Numner" DisplayMemberBinding="{Binding OrderNumber}" Width="100"/>
                    <GridViewColumn Header="Order Date" DisplayMemberBinding="{Binding OrderDate,StringFormat=MMM dd yyyy}" Width="100"/>
                    <GridViewColumn Header="Customer Name" DisplayMemberBinding="{Binding CustomerName}" Width="100"/>
                    <GridViewColumn Header="Address" DisplayMemberBinding="{Binding Address}" Width="200"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

*****************************************************************************

using System;
using System.Collections.Generic;
using System.Windows;

namespace ListViewTutorial
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow" /> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Loaded event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LstSalesData.ItemsSource = GetSalesData();
        }

        /// <summary>
        /// Gets the sales data.
        /// </summary>
        /// <returns></returns>
        private List<SalesData> GetSalesData()
        {
            List<SalesData> lstSalesData = new List<SalesData>();
            try
            {

                Random randNum = new Random();

                String[] states={"Delhi","UP","MP","Ap","Panjap"};
                String[] names = { "Kaushik","Amit","Geeta","Naveen","Priyank","Rahul","Sumit","Pushp","Rohit","Ravi","Anand","Suresh","Rakesh","Ajay","Prabhat"};

                SalesData[] salesdata = new SalesData[10];

                for (int index = 0; index < salesdata.Length; index++)
                {
                    salesdata[index] = new SalesData();
                    salesdata[index].Address = String.Format("House Number  {0}, {1} ", randNum.Next(1, 1000), states[randNum.Next(0, 4)].ToString());
                    salesdata[index].CustomerName = names[randNum.Next(0, 14)].ToString();
                    salesdata[index].OrderDate = DateTime.Now.AddDays(randNum.Next(1,30));
                    salesdata[index].OrderNumber = randNum.Next(1, 1000);
                    lstSalesData.Add(salesdata[index]);
                }
            }
            catch (Exception)
            { 
                throw;
            }
            return lstSalesData;
        }
    }

    internal class SalesData
    {
        public SalesData()
        { 
        
        }

        public int OrderNumber { get; set; }
        public DateTime OrderDate { get; set; }
        public String CustomerName { get; set; }
        public String Address { get; set; }
    }
}
Post a Comment