0

Issue


I have a Horizontal Listbox that contains a List of images that a user can click on to select (See Below)

enter image description here

In this List are 21 images so it creates a scroll bar at the bottom. When selecting an image at the far right of the image above, I would like the scroll bar to move along so that the selected slide is now in the middle. (See Below)

enter image description here

I have tried to add a behaviour to my Listbox which does get hit on slide selection, but does not work.

<ListBox dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" Background="#ececec" SelectedItem="{Binding SelectedSlideDataItem}" ItemsSource="{Binding SlideDataItems}" Grid.Column="2" Grid.Row="10" Grid.RowSpan="4" Grid.ColumnSpan="13" MouseDoubleClick="ListBox_MouseDoubleClick">
    <i:Interaction.Behaviors>
        <behaviors:ListBoxSelectedItemsBehavior/>
    </i:Interaction.Behaviors>



public class ListBoxSelectedItemsBehavior : Behavior<ListBox>
{
    protected override void OnAttached()
    {
        AssociatedObject.SelectionChanged += AssociatedObjectSelectionChanged;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.SelectionChanged -= AssociatedObjectSelectionChanged;
    }

    void AssociatedObjectSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count > 0)
        {
            // Assuming your selection mode is single.
            AssociatedObject.ScrollIntoView(e.AddedItems[0]);
        }
    }
}
Ben Clarke
  • 1,051
  • 4
  • 21
  • 47
  • 1
    `ScrollIntoView` doesn't do anything because it's already in the view. I see your intent is to avoid using scrollbar, so the the user can simply press the rightmost item and then it will be centered, so that user can continue selecting rightmost item to scroll to the right, right? – Sinatr May 25 '16 at 11:55
  • @Sinatr I still want to keep the scrollbar but just so that if you click the far right item it moves into centre :) – Ben Clarke May 25 '16 at 12:00
  • 3
    You'll probably need to do it manually: Handle the SelectionChanged, calculate the item position relative to the listbox and call the scrollviewer scrolltohorizontaloffset – nkoniishvt May 25 '16 at 12:04
  • 1
    See [this](http://stackoverflow.com/q/2946954/1997232). – Sinatr May 25 '16 at 12:06
  • @Sinatr I will take a look now. – Ben Clarke May 25 '16 at 12:44
  • @Sinatr This worked perfectly thanks. – Ben Clarke May 25 '16 at 12:52

0 Answers0