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