Fluent-Gallery: SelectedItem is being set multiple times

Feb 8, 2013 at 12:21 PM
Hi there,

i am using the current version of the source code and noticed a strange behavior while using gallery. I'm basically using the following code:
<fluent:DropDownButton Header="SomeHeader" IsEnabled="{Binding Path=Enabled, Mode=OneWay}">
    <fluent:Gallery ItemsSource="{Binding Path=GalleryItems, Mode=OneWay}" 
    SelectedItem="{Binding Path=GallerySelectedItem, Mode=TwoWay}" />
                    </fluent:DropDownButton>
This code sets GallerySelectedItem:
public MyObject GallerySelectedItem
        {
            set
            {
                if (value == null)
                {
                    return;
                }
        ProcessGalleryItem(value);
            }
        }
I use this to further process the selected value of the gallery. The thing is: the setter of GallerySelectedItem is always called 2 times. This of course results in my method ProcessGalleryItem being called 2 times which isn't that great.

I found out why this happens:
The method "OnClick" in GalleryItem sets "IsSelected = true" which then triggers the static method "OnIsSelectedPropertyChanged". "OnIsSelectedPropertyChanged" has this code:
if((bool)e.NewValue)
            {
                ((GalleryItem)d).BringIntoView();
                Selector parentSelector = ItemsControl.ItemsControlFromItemContainer(((GalleryItem) d)) as Selector;
                if (parentSelector != null) parentSelector.SelectedItem = parentSelector.ItemContainerGenerator.ItemFromContainer(((GalleryItem) d));
            }
As you can see SelectedItem is then again set a second time.
A simple workaround would be:
if (parentSelector != null && parentSelector.SelectedItem != parentSelector.ItemContainerGenerator.ItemFromContainer(((GalleryItem)d))) parentSelector.SelectedItem = parentSelector.ItemContainerGenerator.ItemFromContainer(((GalleryItem)d));
My question: Is there a better way to get notified if a user clicks on the galleryitem? I tried this:
<fluent:DropDownButton Header="SomeHeader" IsEnabled="{Binding Path=Enabled, Mode=OneWay}">
    <fluent:Gallery ItemsSource="{Binding Path=GalleryItems, Mode=OneWay}" 
    SelectedItem="{Binding Path=GallerySelectedItem, Mode=TwoWay}" MouseUp="ProcessGalleryItem"/>
                    </fluent:DropDownButton>
with no success.