ListBoxSelectedItemsBehavior.SelectedItemsSource not working when ListCollectionView is used as ItemsSource

11 posts, 0 answers
  1. Vitalij
    Vitalij avatar
    35 posts
    Member since:
    Dec 2010

    Posted 22 Jul 2015 Link to this post

    Hallo, I have to use ListCollectionView as an ItemsSource for RadListBox and at the same time I have to pass SelectedItems down to my ViewModel. I was trying to use new ListBoxSelectedItemsBehavior.SelectedItemsSource  which is working flawlessly for ObservableCollection<T> as ItemsSource but not at all when using ListCollectionView or CollectionViewSource.

    I have modified a sample from github, simply I've added two more collections for SelectedItems and new ListCollectionView and CollectionViewSource with some simple filters as ItemsSource for additional RadListBoxes. Binding directly to SelectedItems works, but in some cases ElementName binding does not work so to have other approach is ​always good.

    Attached screenshot is from modified sample and as you can see, SelectedItems from additional listboxes contains only items manually added in constructor and not reflecting changes in selection made in associated control.

    I suspect that this is because neither ListCollectionView nor CollectionViewSource is strongly typed collection and CollectionTypeComparerHelper is not detecting that they hold same type as binded SelectedItemsSource collection.

  2. Nasko
    Admin
    Nasko avatar
    587 posts

    Posted 24 Jul 2015 Link to this post

    Hello Vitalij,

    We tried to reproduce the observed by you behavior of RadListBox but it seems everything works as expected on our side - the ItemsSource and SelectedItemsSource are bound as expected to ListCollectionView. We have created a sample project that demonstrates that and you could run and evaluate it - please, check it and let us know if we didn't miss something and if so, please modify the sample project to reproduce the observed by you behavior and send it back to us.

    We also like to inform you that in the current version of the ListBox control using the  the ListBoxSelectedItemsBehavior's SelectedItemsSource is not supported when the bound collection and the ItemsSource are of different types - both the ItemsSource and SelectedItemsSource  need to be of the same type of objects otherwise the SelectedItemsSource is detached. So, please check if the ItemsSource and SelectedItemsSource are bound to the same type of collections. Synchronization will not also work if SelectedItemsSource is bound to a collection that does not implement INotifyCollectionChanged. Please, check the following article from our help documentation that provides some more detailed information:
    http://docs.telerik.com/devtools/wpf/controls/radlistbox/features/selecteditemssource

    Hopes the provided information will help you.

    Regards,
    Nasko
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Vitalij
    Vitalij avatar
    35 posts
    Member since:
    Dec 2010

    Posted 27 Jul 2015 Link to this post

    Thank you for your example. Now I see why it didn't work for me. In my test I was binding SelectedItemsSource to ObservableCollection<T> and not to the ListCollectionView. So apparently both SelectedItemsSource and ItemsSource must be binded not only to the collection of same type of items as is stated in the documentation but strictly to the same type of collection as well. I suggest to find better wording of that information box ​on the documentation page to avoid confusion for other users.

    Regards, Vitalij

     

  5. Vitalij
    Vitalij avatar
    35 posts
    Member since:
    Dec 2010

    Posted 27 Jul 2015 Link to this post

    Actually, after detailed examination of your example I've just spotted that you've binded the ItemSource to the SourceCollection property of ListCollectionView. This renders all filtering and sorting capabilities of ListCollectionView useless.

    In your example, try to add simple textbox binded to FilterText property in ViewModel.cs modified like this:

     

    public ViewModel()
            {
                _filterText = string.Empty;
                this.Items = this.GetItems(100);
                this.SelectedItemsSource = new ObservableCollection<Item>() { this.Items[0], this.Items[2], this.Items[4], this.Items[6], this.Items[7] };
     
                this.ItemsTwo = new ListCollectionView(this.Items)
                {
                    Filter = i =>
                    {
                        if (string.IsNullOrWhiteSpace(FilterText))
                        {
                            return true;
                        }
                        var item = (Item)i;
                        return item.Name.Contains(FilterText);
                    }
                };
                this.SelectedItemsSourceTwo = new ListCollectionView(this.SelectedItemsSource);
            }
     
            public string FilterText
            {
                get { return _filterText; }
                set
                {
                    if (!this._filterText.Equals(value))
                    {
                        _filterText = value;
                        this.OnPropertyChanged(() => this.FilterText);
                        ItemsTwo.Refresh();
                    }
                }
            }

    You'll see that filtering is not applied, but when you change binding of ItemsSource to ItemsTwo filtering works, but SelectedItemsSource does not.

    MainWindows.xaml with working filtering:

    <Window x:Class="RadListBoxListCollectionViewWPF.MainWindow"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <TextBox Grid.Column="0" Grid.Row="0" Margin="10"  Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
            <telerik:RadListBox ItemsSource="{Binding ItemsTwo}"
                                Grid.Column="0" Grid.Row="1"
                                DisplayMemberPath="Name"
                                SelectionMode="Multiple"
                                x:Name="radListBox2"
                                Width="200"
                                Margin="10 0 10 10"
                                telerik:ListBoxSelectedItemsBehavior.SelectedItemsSource="{Binding SelectedItemsSourceTwo.SourceCollection}"/>
            <ItemsControl ItemsSource="{Binding SelectedItemsSourceTwo, Mode=TwoWay}"
                          Margin="20 10 0 0"
                          Grid.Column="1" Grid.Row="1"
                          DisplayMemberPath="Name"/>
            <ItemsControl ItemsSource="{Binding ElementName=radListBox2, Path=SelectedItems}"
                          Margin="20 10 0 0"
                          Grid.Column="2" Grid.Row="1"
                          DisplayMemberPath="Name"/>
     
        </Grid>
    </Window>

  6. Nasko
    Admin
    Nasko avatar
    587 posts

    Posted 29 Jul 2015 Link to this post

    Hello Vitalij,

    After further investigation of the desired by you functionality we determine that binding the SelectedItemsSource property to a collection that inherits the CollectionView class is currently not supported. However, you could log this as feature request in our Feedback Portal. We will review the item and will consider it for implementation.

    However, what we could suggest you as one possible approach to achieve the desired filtering is to bound the ItemsSource and SelectedItemSouce to an ObservableCollection. After you filter the ListCollectionView you could clear the ObservableCollection and fill it with the filtered items. 

    We have created a sample project that demonstrates the described above approach and you could run and evaluate it. 

    We hope this will help you.

    Regards,
    Nasko
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  7. Brad
    Brad avatar
    1 posts
    Member since:
    Apr 2015

    Posted 13 Aug 2015 Link to this post

    Hi folks,

     

    I happened to stumble upon this unsupported issue recently. Have looked thought the feedback portal but couldn't find a related ticket. Has anything been added for this? I would very much like to +1 its existence.

     

    Thanks,

    Brad

  8. Nasko
    Admin
    Nasko avatar
    587 posts

    Posted 14 Aug 2015 Link to this post

    Hello Brad,

    This feature request has not been logged in our Feedback portal. However, you can still log it by yourself and we will consider if for implementation.

    Meanwhile, what we could suggest you is to check the approach suggested in my previous response that demonstrates how you could use the filtering functionality of the collections that inherit CollectionView with a combination of the SelectedItemsSource property.

    We hope that this will help you.

    Regards,
    Nasko
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  9. Vitalij
    Vitalij avatar
    35 posts
    Member since:
    Dec 2010

    Posted 17 Aug 2015 Link to this post

    Hi, I haven't logged this issue in Feedback portal because I needed some solution quickly and I ended up with the change in design so binding to ElementName would work and I can avoid this problem altogether. And frankly, I supposed that no one else would benefit from such feature. Feel free to add one yourself though.

    Regards, Vitalij

  10. AshR
    AshR avatar
    16 posts
    Member since:
    Jan 2015

    Posted 11 Sep 2015 Link to this post

    We also like to inform you that in the current version of the ListBox control using the  the ListBoxSelectedItemsBehavior'sSelectedItemsSource is not supported when the bound collection and the ItemsSource are of different types - both the ItemsSourceand SelectedItemsSource  need to be of the same type of objects otherwise the SelectedItemsSource is detached. So, please check if the ItemsSource and SelectedItemsSource are bound to the same type of collections. 

    This really should be added to the documentation. It is frustrating to find this information buried in the forum when I have just wasted a morning messing about with this issue.

  11. AshR
    AshR avatar
    16 posts
    Member since:
    Jan 2015

    Posted 11 Sep 2015 in reply to AshR Link to this post

    Ahh, I have just seen that this is in fact in the documentation, I had just missed it. 
  12. Nasko
    Admin
    Nasko avatar
    587 posts

    Posted 14 Sep 2015 Link to this post

    Hi Ashley,

    We are glad that you managed to find the need information in our help documentation.

    However, if you you have any other suggestions for improving it please, do not hesitate to contact us.

    Regards,
    Nasko
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WPF is Visual Studio 2017 Ready