3

I am using a Transitionals Slideshow control which has an observable collection of strings bound to the itemsource. These strings are the file paths to each picture in the slidehow. When I first load the WPF app, it runs this method correctly (using a directory path to generate the PicSlideShowCollection):

public void SelectImages(string path)
    {
        // Validate
        if (string.IsNullOrEmpty(path)) throw new ArgumentException("path");

        PicSlideShowCollection.Clear();          


        // Get directory info for specified path
        DirectoryInfo di = new DirectoryInfo(path);

        // Image mask
        string[] extensions = new string[] { "*.jpg", "*.png", "*.gif", "*.bmp" };

        // Search for all
        foreach (string extension in extensions)
        {
            foreach (FileInfo fi in di.GetFiles(extension.ToLower()))
            {
                PicSlideShowCollection.Add(fi.FullName);                    
            }
        }           
    }

However, I have a button that allows the user to change the directory of images to use in the slideshow and re-runs the above method. When that is executed, I get this error:

GeneratorPosition '-1,1' passed to Remove does not have Offset equal to 0.

This occurs on the PicSlideShowCollection.Clear() instruction. If I comment that instruction, the new directory images get ADDED TO the original directory pictures which is NOT what I want.

I know this has to do with the PicSlideShowCollection being used as an item source to the Slide show control, but I need to know how I can prevent this error from occuring.

Thank you!

H.B.
  • 166,899
  • 29
  • 327
  • 400
sunriser
  • 95
  • 1
  • 6

2 Answers2

1
Slideshow.AutoAdvance = false;

Slideshow.SelcetedIndex=-1;

var count=PicSlideShowCollection.Count;

forearch(var item in newsources)
{
 PicSlideShowCollection.Add(item);
}

while(count--)
 PicSlideShowCollection.RemoveAt(0);

Slideshow.SelcetedIndex=0;
Lion
  • 11
  • 1
0

I can't explain why this error occurs. GeneratorPosition is used by the ItemContainerGenerator of an ItemsControl, which should simply work when you bind to its ItemsSource property and add or remove items to/from the source collection. Clearing the source collection is of course also a valid operation.

A possible workaround for the problem would be to reset the ItemsSource each time you switch to another image directory. So instead of clearing the existing collection

PicSlideShowCollection.Clear();

create a new collection and set ItemsSource to the new collection:

PicSlideShowCollection = new ObservableCollection<string>();
slideShowControl.ItemsSource = PicSlideShowCollection;
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Yes, I've already tried that. I had another collection built in the method and tried switching the ItemsSource to it and same error occurs. Could it have something to do with the slideshow iterating one at a time the items in the source and when I update the collection, it is on a certain item position or index? – sunriser Feb 17 '12 at 17:27
  • Yes, but then i would call it a bug in the slideshow control. It should always be possible to modify the source collection. Does the control's documentation say anything about possible states in which changes to the source collection are forbidden? – Clemens Feb 17 '12 at 17:34
  • Unfortunately, No. I read the entire API guide and looked at example code and nowhere is this addressed. Other things I tried: setting control enabled to false then re-enabling, setting itemsource to null then rebinding to collection, Using the Items member of the slideshowcontrol and manually adding items. All produce same error. :( – sunriser Feb 17 '12 at 17:41
  • And unfortunately the Transitionals home page on CodePlex says _Please note that Transitionals does not currently have any active contributors. You are still free to download the binaries or source and use it in your own projects; in fact Transitionals is stable and has been used in several commercial software packages. But please be aware there isn't anyone currently monitoring the discussion groups or resolving issues._ – Clemens Feb 17 '12 at 17:55
  • You could of course get the source code and debug your problem. – Clemens Feb 17 '12 at 17:57