RadComboBox with checkbox with multi select (using MVVM)

2 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
    886 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.
Back to Top