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

Override Selection Change

2 Answers 64 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Manishkumar
Top achievements
Rank 1
Manishkumar asked on 17 Oct 2011, 02:51 PM
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 Answers, 1 is accepted

Sort by
0
Valeri Hristov
Telerik team
answered on 19 Oct 2011, 12:56 PM
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 >>

0
gans
Top achievements
Rank 1
answered on 28 Mar 2012, 02:20 PM
Valeri,

Do you have a working sample of this in WPF? Never Mind. Got it working. 
Tags
ComboBox
Asked by
Manishkumar
Top achievements
Rank 1
Answers by
Valeri Hristov
Telerik team
gans
Top achievements
Rank 1
Share this question
or