Issue
I have a Horizontal Listbox that contains a List of images that a user can click on to select (See Below)
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)
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]);
}
}
}