Skip to content

Instantly share code, notes, and snippets.

@clausjoergensen
Created December 26, 2011 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clausjoergensen/1521560 to your computer and use it in GitHub Desktop.
Save clausjoergensen/1521560 to your computer and use it in GitHub Desktop.
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