自定义CollectionViewType不支持使用shift键

时间:2016-06-17 09:55:48

标签: c# wpf xaml

窗口中,我使用自定义 CollectionViewType CollectionViewSource (<}将 ListBox 中的项目分组EM> EmployeeCollectionView )。

MainWindow.xaml:

<Window x:Class="ListBoxSelectionMode.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ListBoxSelectionMode">
<Window.Resources>
    <CollectionViewSource x:Key="EmployeesSource" Source="{Binding Employees}" CollectionViewType="local:EmployeeCollectionView" />

    <DataTemplate x:Key="EmployeeTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding FirstName}" />
            <TextBlock Text="{Binding LastName}" />
        </StackPanel>
    </DataTemplate>
</Window.Resources>

<ListBox ItemsSource="{Binding Source={StaticResource EmployeesSource}}"
         ItemTemplate="{StaticResource EmployeeTemplate}"
         SelectionMode="Extended">
    <ListBox.GroupStyle>
        <x:Static Member="GroupStyle.Default"/>
    </ListBox.GroupStyle>
</ListBox>

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public IEnumerable<Employee> Employees { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        Employees = new List<Employee>
        {
            new Employee { FirstName = "FirstName1", LastName = "LastName1", HireDate = new DateTime(1970, 1, 1) },
            new Employee { FirstName = "FirstName2", LastName = "LastName2", HireDate = new DateTime(1970, 1, 1) },
            new Employee { FirstName = "FirstName3", LastName = "LastName3", HireDate = new DateTime(1980, 1, 1) },
            new Employee { FirstName = "FirstName4", LastName = "LastName4", HireDate = new DateTime(1980, 1, 1) },
            new Employee { FirstName = "FirstName5", LastName = "LastName5", HireDate = new DateTime(1990, 1, 1) },
            new Employee { FirstName = "FirstName6", LastName = "LastName6", HireDate = new DateTime(1990, 1, 1) }
        };

        DataContext = this;
    }
}

EmployeeCollectionView.cs:

class EmployeeCollectionView : ListCollectionView
{
    private readonly IList<EmployeeCollectionViewGroup> _groups;
    public override ReadOnlyObservableCollection<object> Groups
    {
        get { return new ReadOnlyObservableCollection<object>(new ObservableCollection<object>(_groups)); }
    }

    public EmployeeCollectionView(IList list)
        : base(list)
    {
        _groups = list
             .OfType<Employee>()
            .GroupBy(x => x.HireDate)
            .Select(x => new EmployeeCollectionViewGroup(x.Key, x))
            .ToList();
    }
}

EmployeeCollectionViewGroup.cs:

class EmployeeCollectionViewGroup : CollectionViewGroup
{
    public EmployeeCollectionViewGroup(object name, IEnumerable<object> protectedItems)
        : base(name)
    {
        foreach (var protectedItem in protectedItems)
            ProtectedItems.Add(protectedItem);
    }

    public override bool IsBottomLevel
    {
        get { return true; }
    }
}

问题:为什么我不能使用SHIFT键选择多个项目?

修改:使用CTRLworks进行多项选择。

1 个答案:

答案 0 :(得分:-1)

您是否尝试过按下CTRL键而不是SHIFT键?在声明List之后也移动InitializeComponent。