Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 13th, 2012  |  syntax: None  |  size: 3.08 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Binding multiple progress bars
  2. <ItemsControl Name="MyItemsControl" ItemsSource="{Binding GameDownloads}">
  3.         <ItemsControl.ItemTemplate>
  4.             <DataTemplate>
  5.                 <Grid  DockPanel.Dock="Right">
  6.                     <Grid.RowDefinitions>
  7.                         <RowDefinition />
  8.                         <RowDefinition />
  9.                     </Grid.RowDefinitions>
  10.                     <Grid.ColumnDefinitions>
  11.                         <ColumnDefinition Width="200" />
  12.                         <ColumnDefinition />
  13.                     </Grid.ColumnDefinitions>
  14.  
  15.                     <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding GameName}" />
  16.                     <ProgressBar Grid.Row="0" Grid.Column="1"  Minimum="0" Maximum="100" Value="{Binding Progress, Mode=OneWay}" />
  17.                     <TextBlock Grid.Row="1" Grid.Column="1"  Text="{Binding StatusText}" />
  18.  
  19.                 </Grid>
  20.  
  21.             </DataTemplate>
  22.  
  23.         </ItemsControl.ItemTemplate>
  24.     </ItemsControl>
  25.        
  26. Public Class DownloadAppViewModel
  27. Inherits ViewModelBase
  28.  
  29. Private ReadOnly WC As WebClient
  30. Public Sub New(ByVal Name As String, ByVal URL As String, ByVal FileName As String)
  31.     _gameName = Name
  32.     WC = New WebClient
  33.     AddHandler WC.DownloadFileCompleted, AddressOf DownloadCompleted
  34.     AddHandler WC.DownloadProgressChanged, AddressOf DownloadProgressChanged
  35.     WC.DownloadFileAsync(New Uri(URL), FileName)
  36. End Sub
  37. Private Sub DownloadCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
  38.  
  39. End Sub
  40. Private Sub DownloadProgressChanged(ByVal sender As [Object], ByVal e As DownloadProgressChangedEventArgs)
  41.     If _totalSize = 0 Then
  42.         _totalSize = e.TotalBytesToReceive
  43.     End If
  44.     DownloadedSize = e.BytesReceived
  45. End Sub
  46.  
  47. Private _gameName As String
  48.  
  49. Public Property GameName() As String
  50.     Get
  51.         Return _gameName
  52.     End Get
  53.     Set(ByVal value As String)
  54.         _gameName = value
  55.         Me.OnPropertyChanged("GameName")
  56.     End Set
  57. End Property
  58.  
  59. Public ReadOnly Property StatusText() As String
  60.     Get
  61.         If _downloadedSize < _totalSize Then
  62.             Return String.Format("Downloading {0} MB of {1} MB", _downloadedSize, _totalSize)
  63.         End If
  64.         Return "Download completed"
  65.     End Get
  66. End Property
  67.  
  68. Private _totalSize As Long
  69. Private Property TotalSize() As Long
  70.     Get
  71.         Return _totalSize
  72.     End Get
  73.     Set(ByVal value As Long)
  74.         _totalSize = value
  75.         OnPropertyChanged("TotalSize")
  76.         OnPropertyChanged("Progress")
  77.         OnPropertyChanged("StatusText")
  78.     End Set
  79. End Property
  80.  
  81. Private _downloadedSize As Long
  82. Private Property DownloadedSize() As Long
  83.     Get
  84.         Return _downloadedSize
  85.     End Get
  86.     Set(ByVal value As Long)
  87.         _downloadedSize = value
  88.         OnPropertyChanged("DownloadedSize")
  89.         OnPropertyChanged("Progress")
  90.         OnPropertyChanged("StatusText")
  91.     End Set
  92. End Property
  93.  
  94.  
  95. Public ReadOnly Property Progress() As Double
  96.     Get
  97.         If _totalSize <> 0 Then
  98.             Return 100.0 * _downloadedSize / _totalSize
  99.         Else
  100.             Return 0.0
  101.         End If
  102.  
  103.     End Get
  104.  
  105. End Property
  106.  
  107. End Class