wpf collectionview 多过滤器

wpf collectionview multiple filter

我需要一些建议。我有两个 ListView,第一个 ListView 是 "ListViewAlbums",第二个 "ListViewTracks"。 ListViewTracks 中的项目被组织成组。 ListViewAlbums 设置了 SelectionMode = "Extended" 和 Behaviours:MultiSelectorBehaviours.SynchronizedSelectedItems。 MultiSelectorBehaviours

当我 select 来自 ListViewAlbums 的多个项目时,所以我想显示基于 ALBUMID 的 ListViewTracks 中的所有项目。

我当前的代码:

ListViewTracks 组描述:

 PropertyGroupDescription groupDescription = new PropertyGroupDescription("AlbumID");
 viewTrack.GroupDescriptions.Add(groupDescription);

 viewTrack.SortDescriptions.Add(new System.ComponentModel.SortDescription("AlbumTitle", System.ComponentModel.ListSortDirection.Ascending));
 viewTrack.SortDescriptions.Add(new System.ComponentModel.SortDescription("TitleTrack", System.ComponentModel.ListSortDirection.Ascending));

员工数据:

     private ObservableCollection<EmployeesAlbums> _listAlbums = new ObservableCollection<EmployeesAlbums>();
     public ObservableCollection<EmployeesTracks> _listTrack = new ObservableCollection<EmployeesTracks>();


    public ObservableCollection<EmployeesTracks> PlayListTracks
    {
        get { return _listTrack; }
        set { _listTrack = value; RaisePropertyChanged("PlayListTracks"); }
    }
    public ObservableCollection<EmployeesAlbums> PlayListAlbums
    {
        get { return _listAlbums; }
        set { _listAlbums = value; RaisePropertyChanged("PlayListAlbums"); }
    }
    EmployeesAlbums model = new EmployeesAlbums
    {
       IdAlbums = int.Parse(rdr["Id"].ToString()),
       TitleAlbum = rdr["TitleAlbums"].ToString(),
       ArtistAlbum = rdr["ArtistAlbums"].ToString()
    };
    modelTracks = new EmployeesTracks
    {
       IdTrack = int.Parse(rdr["Id"].ToString()),
       TitleTrack = rdr["TitleTrack"].ToString(),
       PathTrack = rdr["Path"].ToString(),
       AlbumTitle = rdr["AlbumTitle"].ToString(),
       ArtistTrack = rdr["ArtistTrack"].ToString(),
       AlbumID = int.Parse(rdr["AlbumId"].ToString()).ToString()

    };

ListViewAlbums 的选定项:

    SelectionChangedItemsAlbumCommand = new GalaSoft.MvvmLight.CommandWpf.RelayCommand(SelectionChangedItemsAlbum);       

    private EmployeesAlbums _selectedPlayListFileAlbum;       
    public EmployeesAlbums SelectedPlayListFileAlbum
    {
        get { return _selectedPlayListFileAlbum; }
        set
        {

            if (_selectedPlayListFileAlbum != value)
            {
                _selectedPlayListFileAlbum = value;
                RaisePropertyChanged("SelectedPlayListFileAlbum");
            }     
        }
    }

    private IEnumerable<EmployeesAlbums> _selectedPlayListFilesAlbums;
    public IEnumerable<EmployeesAlbums> SelectedPlayListFilesAlbums
    {
        get { return this.selectedPlayListFilesAlbums; }
        set { Set(ref selectedPlayListFilesAlbums, value); }
    }

过滤:

    public string AlbumID { get; set; }
    void SelectionChangedItemsAlbum()
    {

            foreach (var items in SelectedPlayListFilesAlbums)
            {

                ListCollectionView empView = CollectionViewSource.GetDefaultView(PlayListTracks) as ListCollectionView;
                // Enable Live Filtering of the ListViewCollection
                empView.IsLiveFiltering = true;
                // Enable the filtering on AlbumID
                empView.LiveFilteringProperties.Add("AlbumID");
                AlbumID = items.IdAlbums.ToString();
                // Filter based upon AlbumID
                empView.Filter = new Predicate<object>(IsMatchFoundAlbums);
                // Refresh Collection
                empView.Refresh();
            }
    }
    bool IsMatchFoundAlbums(object d)
    {
        bool res = false;
        EmployeesTracks emp = d as EmployeesTracks;
            if (emp.AlbumID == AlbumID)
            {
                res = true;
            }            
        return res;
    }

XAML代码:

<ListView x:Name="ListViewTracks" 

       VirtualizingPanel.ScrollUnit="Pixel"
       VirtualizingStackPanel.CacheLength="20,20"
       VirtualizingStackPanel.CacheLengthUnit="Item"

       ItemsSource="{Binding PlayListTracks}"  
       Style="{StaticResource CommonListViewStyleTracks}"      
       ItemContainerStyle="{DynamicResource styleListViewItem}"      
   >

<ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>

                        <VirtualizingStackPanel/>

                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                        <Setter Property="Control.Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <StackPanel>
                                        <ContentPresenter Content="{TemplateBinding ContentControl.Content}" 
                                        ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                                        ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" />
                                        <ItemsPresenter Margin="0,0,0,20" />
                                    </StackPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate >

                        <Grid d:DesignWidth="460" Height="51">
                            <StackPanel>
                                <TextBlock DataContext="{Binding Items}" Text="{Binding AlbumTitle}" FontSize="18" TextTrimming="CharacterEllipsis" Foreground="{DynamicResource ItemsListViewForeground}"/>
                                <TextBlock DataContext="{Binding Items}" Text="{Binding ArtistTrack}" TextTrimming="CharacterEllipsis" Foreground="{DynamicResource AccentColorApps}"/>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>
        </ListView>

        <ListView 

              x:Name="ListViewAlbums" 
              VirtualizingPanel.IsVirtualizing="True" 
              VirtualizingPanel.VirtualizationMode="Recycling"
              IsSynchronizedWithCurrentItem="True"   
              ItemsSource="{Binding PlayListAlbums}"
              SelectionMode="Extended" 
              SelectedItem="{Binding SelectedPlayListFileAlbum,UpdateSourceTrigger=PropertyChanged}" SelectionMode="Extended" 
              behaviours:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding SelectedPlayListFilesAlbums, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
              Background="{x:Null}" Grid.Column="1" Grid.RowSpan="4" 
              ItemTemplate="{DynamicResource AlbumsDataTemplate}"
              BorderBrush="{x:Null}"
              >
         <ie:Interaction.Triggers >
            <ie:EventTrigger EventName="SelectionChanged">
                <ie:InvokeCommandAction Command="{Binding SelectionChangedItemsAlbumCommand}" 
                        CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"/>
            </ie:EventTrigger>
        </ie:Interaction.Triggers>
        </ListView>

非常感谢。

试试这个

初始化 属性 PlaylistTracks 后,立即执行:

ListCollectionView empView = CollectionViewSource.GetDefaultView(PlayListTracks) as ListCollectionView;
empView.Filter = IsMatchFoundAlbums;

并让您的 Match 方法为:

bool IsMatchFoundAlbums(object d)
{
    EmployeesTracks emp = d as EmployeesTracks;
    return SelectedPlayListFilesAlbums.Any(x => x.IdAlbums == emp.AlbumID);
}