问题


我有一个水平列表框,其中包含用户可以单击以选择的图像列表(参见下文)

enter image description here

在此列表中有21个图像,因此它在底部创建了一个滚动条 . 当选择上面图像最右侧的图像时,我希望滚动条移动,以便所选的幻灯片现在位于中间 . (见下文)

enter image description here

我试图在我的列表框中添加一个行为,它确实会受到幻灯片选择的影响,但是不起作用 .

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