Created
December 26, 2011 16:36
-
-
Save clausjoergensen/1521560 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Example XAML: | |
<ListBox.ItemTemplate> | |
<DataTemplate> | |
<StackPanel> | |
<ProgressBar x:Name="PART_ProgressBar" /> | |
<Image> | |
<Image.Source> | |
<BitmapImage behaviors:ImageDownloadProgress.ProgressBar="{Binding ElementName=PART_ProgressBar}" UriSource="{Binding Image}" /> | |
</Image.Source> | |
</Image> | |
</StackPanel> | |
</DataTemplate> | |
</ListBox.ItemTemplate> | |
C#: | |
using System; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Media.Imaging; | |
namespace Behaviors | |
{ | |
public class ImageDownloadProgress | |
{ | |
public static readonly DependencyProperty ProgressBarProperty = | |
DependencyProperty.RegisterAttached("ProgressBar", | |
typeof(ProgressBar), typeof(BitmapImage), | |
new PropertyMetadata(null, OnProgressBarChanged)); | |
public static ProgressBar GetProgressBar(BitmapImage selector) | |
{ | |
return (ProgressBar)selector.GetValue(ProgressBarProperty); | |
} | |
public static void SetProgressBar(BitmapImage selector, ProgressBar value) | |
{ | |
selector.SetValue(ProgressBarProperty, value); | |
} | |
private static void OnProgressBarChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
{ | |
var image = d as BitmapImage; | |
if (image == null) | |
{ | |
throw new ArgumentException( | |
"You must set the Command attached property on an element that derives from BitmapImage." | |
); | |
} | |
var oldCommand = e.OldValue as ProgressBar; | |
if (oldCommand != null) | |
{ | |
image.DownloadProgress -= DownloadProgress; | |
} | |
var newCommand = e.NewValue as ProgressBar; | |
if (newCommand != null) | |
{ | |
image.DownloadProgress += DownloadProgress; | |
} | |
} | |
private static void DownloadProgress(object sender, DownloadProgressEventArgs e) | |
{ | |
var selector = sender as BitmapImage; | |
var progressBar = GetProgressBar(selector); | |
progressBar.IsIndeterminate = e.Progress != 100; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment