locked
wpf collectionview multiple filter RRS feed

  • Question

  •  

    Hello,
    I need some advice. I have two ListViews first ListView is "ListViewAlbums" and the second "ListViewTracks".

    Items in ListViewTracks are organized into groups.
    ListViewAlbums is set SelectionMode="Extended" and  Behaviours:MultiSelectorBehaviours.SynchronizedSelectedItems.

    MultiSelectorBehaviours

    When I select multiple items from ListViewAlbums, so I want to show all the items in ListViewTracks based ALBUMID.

    My current code:

    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));

    Employees data:

     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()
    
        };

    SelectedItem of 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); }
        }


    Filter:

    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 Code:

    <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>

    Thanks a lot.


     
    Sunday, July 10, 2016 3:20 PM

Answers

  • I solved it:

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

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

    • Marked as answer by ORRNY66 Sunday, July 10, 2016 4:10 PM
    Sunday, July 10, 2016 4:10 PM

All replies

  • The first thing that jumps out at me is that you are using SelectedItem rather than SelectedItems.  If you have a mutliselect then you need all the selected items.  When the property that you bind to the SelectedItems changes then you change the PlayListTracks collection.

    The second thing is that you are using a ListView rather than a ListBox.  Since you don't seem to use the Header properties of the ListView you could just use an ItemTemplate with a ListBox but if that part is working then its really no problem.


    Lloyd Sheen

    Sunday, July 10, 2016 3:49 PM
  • I solved it:

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

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

    • Marked as answer by ORRNY66 Sunday, July 10, 2016 4:10 PM
    Sunday, July 10, 2016 4:10 PM