您可以使用 Prism 的行为:
public class LastSelectionBehavior:Behavior<ListBox>
{
private ICollectionView _itemsSource;
protected override void OnAttached()
{
base.OnAttached();
_itemsSource = AssociatedObject.ItemsSource as ICollectionView;
if (_itemsSource != null)
AssociatedObject.SelectionChanged += AssociatedObjectSelectionChanged;
}
void AssociatedObjectSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
_itemsSource.MoveCurrentTo(e.AddedItems[0]);
}
}
xml:
<ListBox ItemsSource="{Binding Path=NamesView}" SelectionMode="Multiple">
<i:Interaction.Behaviors>
<local:LastSelectionBehavior/>
</i:Interaction.Behaviors>
</ListBox>