wpf collectionview多个过滤器

时间:2016-07-10 14:54:53

标签: c# wpf listview collectionview icollectionview

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

当我从ListViewAlbums中选择多个项目时,我想基于ALBUMID显示ListViewTracks中的所有项目。

enter image description here

我目前的代码:

ListViewTracks GroupDescriptions:

 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的SelectedItem:

    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>

非常感谢。

1 个答案:

答案 0 :(得分:2)

试试这个

在初始化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);
}