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

Multiple select binding

9 Answers 499 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 26 Jul 2012, 02:29 PM
Hi telerik,

I have a collection of objects bound to the a ListBox, and the SelectionMode set to "Multiple".
The objects have a property "IsSelected". 

How do I bind this property to the ListBox?

I want the property of the object to change when the user selects an item.

Thanks for help
Michael

9 Answers, 1 is accepted

Sort by
0
Accepted
Vladi
Telerik team
answered on 31 Jul 2012, 09:48 AM
Hello Michael,

In order to bind your custom property to the ListBox and change its value when the item is selected/unselected you need to customize the RadListBoxItem style and add a two way binding to that custom property.

I have prepared and attached a sample project representing the described approach, give it a try.

Regards,
Vladi
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Michael
Top achievements
Rank 1
answered on 31 Jul 2012, 01:05 PM

Hello Vladi,

Thanks for your reply.

Currently as a workaround I did this:

<telerik:RadListBox.ItemTemplate>
    <DataTemplate>
        <Grid>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding Path=IsSelected}" />
                <Label Content="{Binding Path=DisplayText}" />
            </StackPanel>
        </Grid>
    </DataTemplate>
</telerik:RadListBox.ItemTemplate>


 
But your solution is exactly what I wanted!!! Thanks a lot!

Regards,
Michael

0
Alan
Top achievements
Rank 1
answered on 07 Jan 2013, 08:31 AM
Hi telerik,
I would like to ask a followup question.
I am needing to have another button  "Select All" to select all the item in RadListBox.
I have tried your sample and try to implement it by setting all customer.IsSelected = true when "SelectAll" pressed ;
but it seems not work.
So i would like to ask what is the way to implment the "Select All" button?
Regards
Alan Jim
0
Vladi
Telerik team
answered on 08 Jan 2013, 09:04 AM
Hello Alan,

In order to create a "Select All" button you should create an attached property that will hold a collection of selected items. Then add a button that will add all of the object from the ItemsSource of the control to that collection.

I extended my previous sample project and attached it for you with the described approach. Note that by using attached properties you could easily extend the functionality of any WPF/Silverlight control.

Hope this is helpful.

Regards,
Vladi
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Igor
Top achievements
Rank 1
answered on 27 Feb 2013, 02:08 PM
Hello,

I have tried your example, it works but the initial selection is always false. I expect the "Customer 2" to be selected but it isn't. An initial event overwrite the selection and set it to false. It is something missed in your example for the initial selection?
I am using the Telerik.Windows.Control.dll version 2012.3.1129.40.

Best regards,
Igor
0
Vladi
Telerik team
answered on 28 Feb 2013, 08:53 AM
Hi Igor,

The main goal of my previous example was to show how to create an attached property that will set a collection of items as SelectedItems to the RadListBox control, but it wasn't intended to interact with the IsSelected property of the Customer object in order to select specific items initially. In the MyViewModel.cs file notice that the SelectedItems collection is set to an empty collection, that is way it look that the IsSelected is disregarded. If you want to base the SelectedItems collection on this property all you need to do is filter the items from the Customers collection based on that property before setting the.SelectedItems collection This could be done in the ViewModel.

I updated the previously attached project with the described approach, note that this is an general approach  when creating attached properties and could be implemented in many different ways.

Regards,
Vladi
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Igor
Top achievements
Rank 1
answered on 01 Mar 2013, 09:54 AM
Hi Vladi,

thank you for your example, now the initial selection is the same as the property in view model. Good.
The property 
public ObservableCollection<Customer> SelectedItems

seam only holds the initial selection and it is not updated if Customer IsSelected is changed. It is right?
In my project I use IsSelected property to track the selection, but the type ObservableCollection and the name SelectedItems  confuse me and suggest that selection is also tracked in SelectedItems. It would be great if selection could be manged only with IsSelected in view model. 

Regards
Igor


0
Vladi
Telerik team
answered on 01 Mar 2013, 12:30 PM
Hi Igor,

I would suggest you to read this blog post that shows how attached properties could extend the functionality of any WPF and Silverlight control.

You can find more details about attached properties here.

Regards,
Vladi
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Igor
Top achievements
Rank 1
answered on 01 Mar 2013, 12:50 PM
Hi Vladi,

Thanks a lot.

Igor
Tags
ListBox
Asked by
Michael
Top achievements
Rank 1
Answers by
Vladi
Telerik team
Michael
Top achievements
Rank 1
Alan
Top achievements
Rank 1
Igor
Top achievements
Rank 1
Share this question
or