我有自定义listview tempalte(如下所示):
<ListView.ItemTemplate>
<DataTemplate >
<albc:MultithreadImage
filename="{Binding Path=thumbnailpath,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Name="theImage"/>
</DataTemplate >
</ListView.ItemTemplate>
和ListView的 ItemsSource 绑定到 Photo 类的集合。
自定义控件MultithreadImage可以很好地显示图像,但是当我在Photo类中更改 thumbnailpath 并激活 NotifyPropertyChanged(“thumbnailpath”)时,图像不会在MultithreadImage中更新
Photo class正在实施 INotifyPropertyChanged
有人可以帮忙吗?
检查下面的MultithreadedImage:
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
)
);
}