如何为ListBox SelectionMode获取ListCollectionView的currentItem =“Multiple”它是最后一个selectedItem?

时间:2013-08-05 03:19:10

标签: wpf mvvm wpf-controls

我使用MVVM和ListBox'ItemsSource Binding ListCollectionView Type。

如何获取ListCollectionView的currentItem我希望ListBox是SelectionMode上的Last SelectedItem =“Multiple”

目前,我可以得到第一个selectItem,它是ListCollectionView的currentItem,但是最后的SelectedItem不能得到ListCollectionView的currentItem。

任何人都可以帮助我吗?或者告诉我一些解决方案。

谢谢你。

1 个答案:

答案 0 :(得分:1)

您可以使用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]);
    }
}

的Xaml:

    <ListBox ItemsSource="{Binding Path=NamesView}" SelectionMode="Multiple">
        <i:Interaction.Behaviors>
            <local:LastSelectionBehavior/>
        </i:Interaction.Behaviors>
    </ListBox>