Posted by: Zeeshan Amjad | November 22, 2009

Using progress bar inside the List view


We can easily insert progress bar inside the list view just like we added check box and combo box. We are again going to use the CellTemplate property of GridViewColumn. We are going to define data template to use the progress bar.

This time we are going to do something useful rather than using some dummy sample. This time we are going to make one simple application to calculate the book value of a fixed assets cost using the straight line method.

Here is a class to store the information about the depreciation.

  1: public class DepreciationInfo
  2: {
  3: 	public int Year
  4: 	{ get; set; }
  5: 
  6: 	public double Depreciation
  7: 	{ get; set; }
  8: 
  9: 	public double AccDepreciation
 10: 	{ get; set; }
 11: 
 12: 	public double BookValue
 13: 	{ get; set; }
 14: 
 15: 	public double Percentage
 16: 	{ get; set; }
 17: }
 18: 

Calculation of book value using straight line method is very simple. Here is a simple method to calculate the book value.

  1: double depExpense = depValue / year;
  2: double accDepreciation = 0;
  3: double bookValue = cost;
  4: 
  5: for (int iIndex = 0; iIndex < year; iIndex++)
  6: {
  7: 	accDepreciation += depExpense;
  8: 	bookValue -= depExpense;
  9: 	DepreciationInfo dpInfo = new DepreciationInfo();
 10: 	dpInfo.Year = (iIndex + 1);
 11: 	dpInfo.Depreciation = depExpense;
 12: 	dpInfo.AccDepreciation = accDepreciation;
 13: 	dpInfo.BookValue = bookValue;
 14: 	dpInfo.Percentage = bookValue * 100 / cost;
 15: 
 16: 	depreciation.Add(dpInfo);
 17: }
 18: 
 19: 

Here is a complete XAML code of this project.

  1: <Window x:Class="Depreciation.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Depreciation" Height="400" Width="600">
  5:     <Grid Background="AliceBlue">
  6:         <Grid.RowDefinitions>
  7:             <RowDefinition/>
  8:             <RowDefinition/>
  9:             <RowDefinition/>
 10:             <RowDefinition Height="4*"/>
 11:             <RowDefinition/>
 12:         </Grid.RowDefinitions>
 13:         
 14:         <Grid.ColumnDefinitions>
 15:             <ColumnDefinition/>
 16:             <ColumnDefinition/>
 17:         </Grid.ColumnDefinitions>
 18:         <TextBlock Grid.Column="0" Grid.Row="0" Margin="5" VerticalAlignment="Center">Enter Cost of Fixed Asset</TextBlock>
 19:         <TextBox Grid.Column="1" Grid.Row="0" Margin="5" VerticalAlignment="Center" Name="txtCost"></TextBox>
 20:         <TextBlock Grid.Column="0" Grid.Row="1" Margin="5" VerticalAlignment="Center">Enter life Span</TextBlock>
 21:         <TextBox Grid.Column="1" Grid.Row="1" Margin="5" VerticalAlignment="Center" Name="txtYear"></TextBox>
 22:         <TextBlock Grid.Column="0" Grid.Row="2" Margin="5" VerticalAlignment="Center">Enter Scrap Value</TextBlock>
 23:         <TextBox Grid.Column="1" Grid.Row="2" Margin="5" VerticalAlignment="Center" Name="txtScrapValue"></TextBox>
 24:         
 25:         <ListView Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" Name="list" Margin="5" HorizontalContentAlignment="Stretch">
 26:             <ListView.View>
 27:                 <GridView>
 28:                     <GridView.ColumnHeaderTemplate>
 29:                         <DataTemplate>
 30:                             <Border BorderBrush="Brown" BorderThickness="2" CornerRadius="5">
 31:                                 <Border.Background>
 32:                                     <LinearGradientBrush>
 33:                                         <GradientStop Offset="0" Color="Wheat"/>
 34:                                         <GradientStop Offset="1" Color="LightCoral"/>
 35:                                     </LinearGradientBrush>
 36:                                 </Border.Background>
 37:                                 <TextBlock Foreground="Blue" FontSize="14" FontWeight="Bold" Margin="5" Text="{Binding}" Width="Auto"/>
 38:                             </Border>
 39:                         </DataTemplate>
 40:                     </GridView.ColumnHeaderTemplate>
 41:                     <GridViewColumn Width="Auto" Header="Year" DisplayMemberBinding="{Binding Path=Year}"/>
 42:                     <GridViewColumn Width="Auto" Header="Depreciation" DisplayMemberBinding="{Binding Path=Depreciation}"/>
 43:                     <GridViewColumn Width="Auto" Header="Accumulated Depreciation" DisplayMemberBinding="{Binding Path=AccDepreciation}"/>
 44:                     <GridViewColumn Width="Auto" Header="Book Value" DisplayMemberBinding="{Binding Path=BookValue}"/>
 45:                     <GridViewColumn Width="Auto" Header="Percentage">
 46:                         <GridViewColumn.CellTemplate>
 47:                             <DataTemplate>
 48:                                 <ProgressBar Width="50" Height="20" Margin="5" Minimum="0" Maximum="100" Value="{Binding Percentage}"/>
 49:                             </DataTemplate>
 50:                         </GridViewColumn.CellTemplate>
 51:                     </GridViewColumn>
 52:                 </GridView>
 53:             </ListView.View>
 54:         </ListView>
 55:         <Button Grid.Column="0" Grid.Row="4" Margin="10" Width="75" Name="btnCalculate" Click="btnCalculate_Click">Calculate</Button>
 56:         <Button Grid.Column="1" Grid.Row="4" Margin="10" Width="75" Name="btnExit" Click="btnExit_Click">Exit</Button>
 57:     </Grid>
 58: </Window>
 59: 

Here is a complete C# code of this project.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Windows;
  6: using System.Windows.Controls;
  7: using System.Windows.Data;
  8: using System.Windows.Documents;
  9: using System.Windows.Input;
 10: using System.Windows.Media;
 11: using System.Windows.Media.Imaging;
 12: using System.Windows.Navigation;
 13: using System.Windows.Shapes;
 14: 
 15: namespace Depreciation
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 22:         List<DepreciationInfo> depreciation;
 23:         private int year;
 24:         private double cost;
 25:         private double scrap;
 26: 
 27:         public Window1()
 28:         {
 29:             InitializeComponent();
 30: 
 31:             depreciation = new List<DepreciationInfo>();
 32:         }
 33: 
 34:         private void btnExit_Click(object sender, RoutedEventArgs e)
 35:         {
 36:             Close();
 37:         }
 38: 
 39:         private void btnCalculate_Click(object sender, RoutedEventArgs e)
 40:         {
 41:             depreciation.Clear();
 42: 
 43:             year = Convert.ToInt32(txtYear.Text);
 44:             cost = Convert.ToDouble(txtCost.Text);
 45:             scrap = Convert.ToDouble(txtScrapValue.Text);
 46: 
 47:             if (year <= 0)
 48:             {
 49:                 MessageBox.Show("Number of years can not be zero or negative.");
 50:                 return;
 51:             }
 52: 
 53:             double depValue = cost - scrap;
 54: 
 55:             if (depValue <= 0)
 56:             {
 57:                 MessageBox.Show("Either Cost or Scrap value is not correct.");
 58:                 return;
 59:             }
 60: 
 61:             double depExpense = depValue / year;
 62:             double accDepreciation = 0;
 63:             double bookValue = cost;
 64: 
 65:             for (int iIndex = 0; iIndex < year; iIndex++)
 66:             {
 67:                 accDepreciation += depExpense;
 68:                 bookValue -= depExpense;
 69:                 DepreciationInfo dpInfo = new DepreciationInfo();
 70:                 dpInfo.Year = (iIndex + 1);
 71:                 dpInfo.Depreciation = depExpense;
 72:                 dpInfo.AccDepreciation = accDepreciation;
 73:                 dpInfo.BookValue = bookValue;
 74:                 dpInfo.Percentage = bookValue * 100 / cost;
 75: 
 76:                 depreciation.Add(dpInfo);
 77:             }
 78: 
 79:             list.ItemsSource = depreciation;
 80:         }
 81:     }
 82: 
 83:     public class DepreciationInfo
 84:     {
 85:         public int Year
 86:         { get; set; }
 87: 
 88:         public double Depreciation
 89:         { get; set; }
 90: 
 91:         public double AccDepreciation
 92:         { get; set; }
 93: 
 94:         public double BookValue
 95:         { get; set; }
 96: 
 97:         public double Percentage
 98:         { get; set; }
 99:     }
100: }
101: 

Here is the output of this program.

Depreciation

Advertisements

Responses

  1. Can you please send me the code through an attachment.

    • Sure I will no problem.

      Regards
      Zeeshan Amjad

  2. Hi,

    Your code is excellent I want to implement this code in my current project where there is requirement like this. Please send me the code through an attachment at oliveson@yahoo.com.

    Thanks
    John Thalluri-Solomon

    • Hi John

      Thanks to like it and please check your email. I sent the code to your email address.

      Regards
      Zeeshan Amjad

  3. Hi Zeeshan,

    I need the same thing but in MVVM way. Do you have another version of it for MVVM?? or if you can help me in updating progressbar from Viewmodel??

    Regards,
    Rani

    • Hi Rani

      Yes you can do it in view model too. Please contact me via email, i will send you the simple project. Thanks for visiting my blog.

      Regards
      Zeeshan Amjad

  4. i would like to have this code as well. i tried to do but ive receiving red lines. may i have the codes as well?

    • Please give me your email address so i can send you the project.

      • random_16@live.com
        Thank you so much.

      • Please check your email.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: