RadComboBox with checkbox with multi select (using MVVM)

7 posts, 0 answers
  1. Shane
    Shane avatar
    4 posts
    Member since:
    Mar 2018

    Posted 18 Jul 2018 Link to this post

    I have a radcombobox that has a list of checkboxes that has multiselect. 

    -- template for the checkbox
    <UserControl.Resources>
            <DataTemplate x:Key="MultiSelectComboTemplate">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
     
                    <StackPanel Orientation="Horizontal">
                        <CheckBox x:Name="checkbox" IsChecked="{Binding IsSelected}" Command="{Binding ElementName=_cboTeams, Path=DataContext.SelectCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:RadComboBox}}}"/>
                         
                        <TextBlock Text="{Binding Name}" Margin="5 0 0 0"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
            <CollectionViewSource x:Key="teams" Source="{Binding Teams}"/>
             
        </UserControl.Resources>

     

    -- radcombobox
     <telerik:RadComboBox x:Name="_cboTeams"  Grid.Row="1" Grid.Column="2" Margin="5 0 5 0" Width="120"
                                 SelectedValuePath="Id"
                                 ItemsSource="{Binding Teams}"
                                 AllowMultipleSelection="True"
                                 ItemTemplate="{StaticResource MultiSelectComboTemplate}"
                                 DropDownClosed="_cboTeams_DropDownClosed"
                                 Command="{Binding ElementName=_cboTeams, Path=DataContext.SelectCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}">
            </telerik:RadComboBox>

     

    Two questions. 

     

    1. I find that I need to get the most current item selected. With that said, how do I get the most current row(either by Id or by Index)

     

    2. Second, my radcombobox has two types of "events" i need to worry about. One is when the row is clicked and highlighted and another one when user clicks the checkbox itself. If I click the row , I manually search for the item on the Viewmodel and mark the item checked to check the checkbox of that row. My issue is, if the user clicks the checkbox, I can trigger the command of the viewmodel and I will pass the s

     

    private void OnSelect(object sender)
            {           
                if (sender is CheckBox)
                {
                    var obj = sender as CheckBox;
                    var team = (LabeledValueCheckBox)obj.DataContext;
                    if (team.IsSelected)
                    {
                        if (team.Id == 0)
                        {
                            foreach (var t in this.Teams)
                            {
                                t.IsSelected = true;
                            }
                        }
                    }
                    else
                    {
                        foreach (var t in this.Teams)
                        {
                            t.IsSelected = false;
                        }
                    }
                }
                else
                {
                    var obj = sender as RadComboBox;
                    var contxt = (AddFirmViewModel)obj.DataContext;
                    var selectedTeams = obj.SelectedItems;
     
                    if(obj.SelectedItems == null)
                    {
                        foreach(LabeledValueCheckBox t in obj.ItemsSource)
                        {
                            if (t.IsSelected)
                            {
                                selectedTeams.Add(t);
                            }
                        }
                    }
     
     
     
                    if (selectedTeams != null)
                    {
                        if (selectedTeams.Contains(contxt.Teams[0]))// select all
                        {
                            foreach (var t in contxt.Teams)
                            {
                                t.IsSelected = true;
                                selectedTeams.Add(t);
                            }
                        }
                        else
                        {
                            foreach (var t in contxt.Teams)
                            {
                                var exists = selectedTeams.Contains(t);
     
                                if (exists)
                                {
                                    t.IsSelected = true;
                                }
                                else
                                {
                                    t.IsSelected = false;
                                }
                            }
                        }
                    }
                }
                 
                 
                SelectPressed?.Invoke(this, new EventArgs());
            }

     

     

  2. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    934 posts

    Posted 23 Jul 2018 Link to this post

    Hello Shane,

    I assume there was some issue when sending your message as I you second inquiry ends mid-sentence. Could you please try sending it again?

    As for your first inquiry, I would suggest keeping the selected elements in a Stack collection (last-in-first-out method) so that you can use the Pop or Peek methods to get the last-added item. Please let me know whether this would work for you.

    If possible, please open a new support ticket with a small sample project representing the setup at your end so that I can better assist you with your second inquiry.

    I look forward to your reply.

    Regards,
    Dilyan Traykov
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
  3. raneesras
    raneesras avatar
    86 posts
    Member since:
    Nov 2018

    Posted 09 Jul Link to this post

    hiii is it possible to add check all option in Radcombobox with checkbox ??? would u give me a sample project related to this ?

     

    regards,

    Ranees ras

  4. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    934 posts

    Posted 10 Jul Link to this post

    Hi Ranees,

    Can you please clarify whether you want the check-all checkbox to be inside the RadComboBox or outside of it?

    I've prepared a small sample project demonstrating the first scenario. It is based on the "Selected Items Binding" demo from our SDK Samples Browser. The example demonstrates how to utilize the SelectAll and UnselectAll methods of the RadComboBox control. If you want to place this checkbox inside the RadComboBox control, you will need to edit its control template accordingly.

    Alternatively, you can data-bind the IsSelected property of the RadComboBoxItems to a property of your business objects and move the logic for selecting all items in your viewmodel.

    Please let me know if you find this example helpful or if I have misunderstood your requirement in any way.

    Regards,
    Dilyan Traykov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  5. raneesras
    raneesras avatar
    86 posts
    Member since:
    Nov 2018

    Posted 10 Jul in reply to Dilyan Traykov Link to this post

    hiii...Dilyan Traykov....i want to add check-all checkbox inside of the Radcombobox...i am sorry i forget to mention that

    regards,

    ranees ras

  6. raneesras
    raneesras avatar
    86 posts
    Member since:
    Nov 2018

    Posted 11 Jul in reply to Dilyan Traykov Link to this post

    if u don't mind please give me a sample project

    regards,

    ranees ras

  7. Dinko
    Admin
    Dinko avatar
    889 posts

    Posted 15 Jul Link to this post

    Hi,

    My name is Dinko and I'm stepping in for my colleague Dilyan, who is currently out of the office.

    Thank you for the clarification.

    I've updated the project so that the CheckBox is placed inside the control's dropdown.

    Please have a look and let me know if this provides the desired result.

    Regards,
    Dinko
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top