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

RadComboBox with checkbox with multi select (using MVVM)

6 Answers 521 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Shane
Top achievements
Rank 1
Shane asked on 19 Jul 2018, 03:27 AM

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());
        }

 

 

6 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 23 Jul 2018, 10:50 AM
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.
0
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 09 Jul 2019, 12:03 PM

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

0
Dilyan Traykov
Telerik team
answered on 10 Jul 2019, 02:02 PM
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.
0
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 11 Jul 2019, 04:39 AM

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

0
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 12 Jul 2019, 04:23 AM

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

regards,

ranees ras

0
Dinko
Telerik team
answered on 15 Jul 2019, 10:58 AM
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.
Tags
ComboBox
Asked by
Shane
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
Dinko
Telerik team
Share this question
or