Multiple select binding

10 posts, 1 answers
  1. Michael
    Michael avatar
    4 posts
    Member since:
    Jan 2012

    Posted 26 Jul 2012 Link to this post

    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
  2. Answer
    Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 31 Jul 2012 Link to this post

    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.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Michael
    Michael avatar
    4 posts
    Member since:
    Jan 2012

    Posted 31 Jul 2012 Link to this post

    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

  5. Alan
    Alan avatar
    2 posts
    Member since:
    Jun 2012

    Posted 07 Jan 2013 Link to this post

    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
  6. Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 08 Jan 2013 Link to this post

    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.

  7. Igor
    Igor avatar
    26 posts
    Member since:
    Nov 2010

    Posted 27 Feb 2013 Link to this post

    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
  8. Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 28 Feb 2013 Link to this post

    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.

  9. Igor
    Igor avatar
    26 posts
    Member since:
    Nov 2010

    Posted 01 Mar 2013 Link to this post

    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


  10. Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 01 Mar 2013 Link to this post

    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.

  11. Igor
    Igor avatar
    26 posts
    Member since:
    Nov 2010

    Posted 01 Mar 2013 Link to this post

    Hi Vladi,

    Thanks a lot.

    Igor
Back to Top
UI for WPF is Visual Studio 2017 Ready