Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > ComboBox > Override Selection Change

Not answered Override Selection Change

Feed from this thread
  • Manishkumar Master avatar

    Posted on Oct 17, 2011 (permalink)

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

    Reply

  • Valeri Hristov Valeri Hristov admin's avatar

    Posted on Oct 19, 2011 (permalink)

    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 >>

    Attached files

    Reply

  • gans avatar

    Posted on Mar 28, 2012 (permalink)

    Valeri,

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

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > ComboBox > Override Selection Change
Related resources for "Override Selection Change"

WPF ComboBox Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  ]