Override Selection Change

3 posts, 0 answers
  1. Manishkumar
    Manishkumar avatar
    240 posts
    Member since:
    Feb 2009

    Posted 17 Oct 2011 Link to this post

    I have developed a custom control for multiple selection in Rad Combobox. Now i want to show the selected items as comma separated string. Can i manipulate the display member of rad combo box at runtime?
    <Window.Resources>
            <DataTemplate x:Key="ItemContDataTemplateKey">
                <Grid x:Name="radCmb" Width="210" HorizontalAlignment="Left">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition  Width="20"></ColumnDefinition>
                        <ColumnDefinition  Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <CheckBox x:Name="chkSelct" HorizontalAlignment="Left" DataContext="{Binding}" Grid.Column="0" Unchecked="chkSelct_Unchecked" Checked="chkSelct_Checked"></CheckBox>
                    <TextBlock x:Name="txtDropDown" Text="{Binding FullName}" Grid.Column="1"></TextBlock>
                </Grid>
            </DataTemplate>
    </Window.Resources>
      
    <Telerik:RadComboBox Name="radCmbCompany" HorizontalAlignment="Center" Height="25" Width="250" 
                                 SelectedValuePath="Id"
                                 IsEditable="True"
                                 IsReadOnly="True"
                                 ItemTemplate="{StaticResource ItemContDataTemplateKey}" 
                                 >
      
            </Telerik:RadComboBox>
      
      
    //CODE BEHIND
      
    public string DisplayMember { get; set; }
      
    public static readonly DependencyProperty SelectedItemsProperty =
            DependencyProperty.Register("SelectedItems", typeof(ObservableCollection<object>), typeof(Window1),
            new FrameworkPropertyMetadata(new ObservableCollection<object>()));
            public ObservableCollection<object> SelectedItems
            {
                get { return (ObservableCollection<object>)base.GetValue(SelectedItemsProperty); }
                set { base.SetValue(SelectedItemsProperty, value); }
            }
      
            public static readonly DependencyProperty SelectedTextProperty =
            DependencyProperty.Register("SelectedText", typeof(string), typeof(Window1),
            new FrameworkPropertyMetadata());
            public string SelectedText
            {
                get { return (string)base.GetValue(SelectedTextProperty); }
                set { base.SetValue(SelectedTextProperty, value); }
            }
    private void chkSelct_Checked(object sender, RoutedEventArgs e)
            {
                SetSelectedText(sender);
            }
      
            private void chkSelct_Unchecked(object sender, RoutedEventArgs e)
            {
                SetSelectedText(sender);
            }
      
    private void SetSelectedText(object sender)
            {
                CheckBox chkbx = sender as CheckBox;
      
                if (chkbx != null && chkbx.IsChecked.Equals(true))
                {
                    this.SelectedItems.Add(chkbx.DataContext);
                    string CurrentselectedText = Convert.ToString(chkbx.DataContext.GetType().GetProperty(this.DisplayMember).GetValue(chkbx.DataContext, null));
                    if (this.SelectedText != null && this.SelectedText.Length > 0)
                    {
                        this.SelectedText += "," + CurrentselectedText;
                    }
                    else
                    {
                        this.SelectedText = CurrentselectedText;
                    }
                }
                else if (chkbx != null && chkbx.IsChecked.Equals(false))
                {
                    string CurrentselectedText = Convert.ToString(chkbx.DataContext.GetType().GetProperty(this.DisplayPath).GetValue(chkbx.DataContext, null));
                    if (this.SelectedText != null && this.SelectedText.Length > 0 && this.SelectedText.IndexOf(',') > 0)
                    {
                        if (this.SelectedText.IndexOf(CurrentselectedText) + CurrentselectedText.Length < this.SelectedText.Length && this.SelectedText.Substring(this.SelectedText.IndexOf(CurrentselectedText) + CurrentselectedText.Length, 1) == ",")
                        {
                            this.SelectedText = this.SelectedText.Remove(this.SelectedText.IndexOf(CurrentselectedText), CurrentselectedText.Length + 1);
                        }
                        else
                        {
                            this.SelectedText = this.SelectedText.Remove(this.SelectedText.IndexOf(CurrentselectedText) + 1, CurrentselectedText.Length);
                        }
                    }
                    else if (this.SelectedText != null && this.SelectedText.Length > 0)
                    {
                        this.SelectedText = string.Empty;
                    }
                }
            }
  2. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 19 Oct 2011 Link to this post

    Hello Manishkumar,

    We have an example that demonstrates how to create multiselection combobox, I hope it will be of help, so please find it attached.

    Regards,
    Valeri Hristov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

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

    Posted 28 Mar 2012 Link to this post

    Valeri,

    Do you have a working sample of this in WPF? Never Mind. Got it working. 
Back to Top