This is a migrated thread and some comments may be shown as answers.

ListBoxSelectedItemsBehavior.SelectedItemsSource not working when ListCollectionView is used as ItemsSource

10 Answers 427 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Vladimir
Top achievements
Rank 1
Vladimir asked on 22 Jul 2015, 11:50 AM

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.

10 Answers, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 24 Jul 2015, 02:58 PM
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
0
Vladimir
Top achievements
Rank 1
answered on 27 Jul 2015, 11:32 AM

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

 

0
Vladimir
Top achievements
Rank 1
answered on 27 Jul 2015, 12:14 PM

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>

0
Nasko
Telerik team
answered on 29 Jul 2015, 01:49 PM
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
0
Brad
Top achievements
Rank 1
answered on 14 Aug 2015, 03:44 AM

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

0
Nasko
Telerik team
answered on 14 Aug 2015, 11:40 AM
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
0
Vladimir
Top achievements
Rank 1
answered on 17 Aug 2015, 09:11 AM

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

0
AshR
Top achievements
Rank 1
answered on 11 Sep 2015, 11:09 AM

[quote]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. [/quote]

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.

0
AshR
Top achievements
Rank 1
answered on 11 Sep 2015, 11:14 AM
Ahh, I have just seen that this is in fact in the documentation, I had just missed it. 
0
Nasko
Telerik team
answered on 14 Sep 2015, 01:02 PM
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
Tags
ListBox
Asked by
Vladimir
Top achievements
Rank 1
Answers by
Nasko
Telerik team
Vladimir
Top achievements
Rank 1
Brad
Top achievements
Rank 1
AshR
Top achievements
Rank 1
Share this question
or