I have a custom listview tempalte ( something like below):
<ListView.ItemTemplate>
<DataTemplate >
<albc:MultithreadImage
filename="{Binding Path=thumbnailpath,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Name="theImage"/>
</DataTemplate >
</ListView.ItemTemplate>
and ListView's ItemsSource is binding to a collection of Photo classes.
The custom control MultithreadImage displays images well, but when I change thumbnailpath in Photo class and fire NotifyPropertyChanged("thumbnailpath") the image does not update in MultithreadImage
Photo class is implementing INotifyPropertyChanged
Anybody can help?
Check for MultithreadedImage below :
class MultithreadImage:System.Windows.Controls.Image
{
public string filename
{
get {
return (string)GetValue(filenameProperty); }
set {
SetValue(filenameProperty, value);
}
}
public string temp = "";
BitmapImage source2;
private static void OnFilenameChanged(DependencyObject defectImageControl, DependencyPropertyChangedEventArgs eventArgs)
{
var control = (MultithreadImage)defectImageControl;
control.temp = control.filename;
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (o, e) =>
{
try
{
//http://stackoverflow.com/questions/6430299/bitmapimage-in-wpf-does-lock-file
var bmi = new BitmapImage();
bmi.BeginInit();
bmi.CacheOption = BitmapCacheOption.OnLoad;
bmi.UriSource = new Uri(control.temp);
bmi.EndInit();
control.source2 = bmi;
control.source2.Freeze();
}
catch
{
}
};
bw.RunWorkerCompleted += (o, e) =>
{
if (control.source2 != null)
control.Source = control.source2;
};
bw.RunWorkerAsync(control);
}
// Using a DependencyProperty as the backing store for filename. This enables animation, styling, binding, etc...
public static readonly DependencyProperty filenameProperty =
DependencyProperty.Register("filename", typeof(string), typeof(MultithreadImage),
new FrameworkPropertyMetadata(
// use an empty Guid as default value
"",
// tell the binding system that this property affects how the control gets rendered
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
// run this callback when the property changes
OnFilenameChanged
)
);
}