1

我有一个自定义列表视图模板(如下所示):

<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 中更新

照片类正在实现INotifyPropertyChanged

有人可以帮忙吗?

检查下面的多线程图像:

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
      )
    );


    }
4

0 回答 0