1

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


    }
AVEbrahimi
  • 17,993
  • 23
  • 107
  • 210
  • No idea what a albc:MultithreadImage is, but it's quite likely that it doesn't respond to changes in the 'filename' property as a means to change the displayed image. – RJ Lohan Jun 11 '13 at 06:28
  • @RJLohan I just added MultithreadedImage to question – AVEbrahimi Jun 11 '13 at 06:31
  • Have you debugged this? Put a breakpoint in `OnFilenameChanged` to verify it is even being called? – RJ Lohan Jun 11 '13 at 06:35
  • @RJLohan just the first time it is called and displays initial thumbnail, but when I change thumbnail property of Photo nothing happens and nobody is called! – AVEbrahimi Jun 11 '13 at 06:36
  • Does the photo class definition look like "class Photo : INotifyPropertyChanged" just implementing raisepropertychanged without specifying the interface in class definition is kinda useless – Viv Jun 11 '13 at 06:51
  • do you really need that approach? Normally it should be enough to use a normal `Image` and bind the `Source`-Property to the `thumbnailpath`-Property of your viewmodel – Jehof Jun 11 '13 at 07:05
  • Are you setting the property `thumbnailpath` through property setter method? Or are you setting the private filed directly instead of setting the property? – Colin Jun 11 '13 at 07:06
  • 1
    Please also show the implementation of the `thumbnailpath` property. And by the way, you should *at least* write a warning message in the catch block in DoWork. And the comment at the default value for the `filename` property is an excellent example of why extensive code commenting is bad. – Clemens Jun 11 '13 at 07:07
  • `And the comment at the default value for the filename property is an excellent example of why extensive code commenting is bad.` That made me lol. Very true. At this level of commenting I'd expect the actual default value to be `string.Empty` than `""` tbh. – Viv Jun 11 '13 at 10:36

0 Answers0