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

RadComboBox NullValueTemplate

2 Answers 131 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Jose Jones
Top achievements
Rank 1
Jose Jones asked on 19 Feb 2010, 03:00 PM
Hi...

I would like to inherit from the RadComboBox to have a NullableComboBox which allows us to set a distinct template for anull value within the combobox declaration.

Something like this

<CustomControls:RadCustomComboBox   
                 ItemsSource="{Binding SomeEntities, Converter={StaticResource nullableItemSourceConverter}}">  
          <CustomControls:RadCustomComboBox.NullValueContentTemplate> 
            <DataTemplate> 
              <TextBlock Text="Something representing a null value" /> 
            </DataTemplate> 
          </CustomControls:RadCustomComboBox.NullValueContentTemplate> 
          <CustomControls:RadCustomComboBox.ItemTemplate> 
            <DataTemplate> 
              <StackPanel Orientation="Horizontal">  
                <TextBlock Text="{Binding Path=Attribute1}" /> 
                <TextBlock Text="{Binding Path=Attribute2}" Foreground="DarkRed"  /> 
              </StackPanel> 
            </DataTemplate> 
          </CustomControls:RadCustomComboBox.ItemTemplate> 
        </CustomControls:RadCustomComboBox> 

The Null value is inserted in the list using a quite simple converter as I don't want my ViewModel to expose a list with a null value.

I'm able to display the templated item.  This works fine.  Unfortunatly, I'm just not able to get the SelectionChanged event to be raised when I select it.  I was able to do this stuff using a regular ComboBox and I noticed that the PrepareContainerForItemOverride method is raised earlier in the comboxBox than in the RadComboBox (which is raised only when the user clicks on the combobox).

I'm requesting some help, I would greatly appreciate to benefit from the rich feature set of the radcombobox.

Here's the code for the custom control

 public class RadCustomComboBox : RadComboBox  
    {  
        public static readonly DependencyProperty NullValueContentProperty = DependencyProperty.Register("NullValueContent"typeof(object), typeof(RadCustomComboBox), new PropertyMetadata(new PropertyChangedCallback(RadCustomComboBox.OnNullValueContentPropertyChanged)));  
        public static readonly DependencyProperty NullValueContentTemplateProperty = DependencyProperty.Register("NullValueContentTemplate"typeof(DataTemplate), typeof(RadCustomComboBox), new PropertyMetadata(new PropertyChangedCallback(RadCustomComboBox.OnNullValueContentTemplatePropertyChanged)));  
        protected RadComboBoxItem NullValueListBoxItem { getset; }  
 
 
        /// <summary>  
        /// Gets or sets the NullValueContent value.  
        /// </summary>  
        public object NullValueContent  
        {  
            get { return (object)base.GetValue(RadCustomComboBox.NullValueContentProperty); }  
            set { base.SetValue(RadCustomComboBox.NullValueContentProperty, value); }  
        }  
 
        /// <summary>  
        /// Gets or sets the NullValueContentTemplate value.  
        /// </summary>  
        public DataTemplate NullValueContentTemplate  
        {  
            get { return (DataTemplate)base.GetValue(RadCustomComboBox.NullValueContentTemplateProperty); }  
            set { base.SetValue(RadCustomComboBox.NullValueContentTemplateProperty, value); }  
        }  
 
 
        /// <summary>  
        /// Initializes a new instance of the <see cref="RadCustomComboBox"/> class.  
        /// </summary>  
        public RadCustomComboBox()  
            : base()  
        {  
            base.SelectionChanged += new SelectionChangedEventHandler(RadCustomComboBox_SelectionChanged);  
        }  
 
        /// <summary>  
        /// Handles the SelectionChanged event of the RadCustomComboBox control.  
        /// </summary>  
        /// <param name="sender">The source of the event.</param>  
        /// <param name="e">The <see cref="Telerik.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>  
        private void RadCustomComboBox_SelectionChanged(Object sender, SelectionChangedEventArgs e)  
        {  
            if (this.SelectedItem == this.NullValueListBoxItem)  
            {  
                this.SelectedItem = null;  
                this.SelectedValue = null;  
            }  
 
            if (!string.IsNullOrEmpty(SelectedValuePath))  
            {  
                object value = null;  
                if (SelectedItem != null)  
                    value = SelectedItem.GetType().GetProperty(SelectedValuePath).GetValue(SelectedItem, null);  
 
                SetValue(RadCustomComboBox.SelectedValueProperty, value);  
            }  
        }  
 
        /// <summary>  
        /// Invoked when the <see cref="P:System.Windows.Controls.ItemsControl.Items"/> property changes.  
        /// </summary>  
        /// <param name="e">Information about the change.</param>  
        protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)  
        {  
            switch (e.Action)  
            {  
                case NotifyCollectionChangedAction.Reset:  
                    this.NullValueListBoxItem = null;  
                    break;  
                case NotifyCollectionChangedAction.Remove:  
                case NotifyCollectionChangedAction.Replace:  
                    if (e.OldItems.Contains(null)) this.NullValueListBoxItem = null;  
                    break;  
            }  
            object obj = this.SelectedItem;  
            base.OnItemsChanged(e);  
 
            this.SelectedItem = obj;  
 
        }  
 
        /// <summary>  
        /// Called when [null value content changed].  
        /// </summary>  
        /// <param name="oldValue">The old value.</param>  
        /// <param name="newValue">The new value.</param>  
        private void OnNullValueContentChanged(object oldValue, object newValue)  
        {  
            if (this.NullValueListBoxItem != null)  
            {  
                this.NullValueListBoxItem.Content = newValue;  
                this.OnApplyTemplate();  
            }  
        }  
 
        /// <summary>  
        /// Called when [null value content template property changed].  
        /// </summary>  
        /// <param name="d">The d.</param>  
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>  
        private static void OnNullValueContentTemplatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)  
        {  
            ((RadCustomComboBox)d).OnNullValueContentTemplateChanged((DataTemplate)e.OldValue, (DataTemplate)e.NewValue);  
        }  
 
 
        /// <summary>  
        /// Called when [null value content template changed].  
        /// </summary>  
        /// <param name="oldValue">The old value.</param>  
        /// <param name="newValue">The new value.</param>  
        private void OnNullValueContentTemplateChanged(DataTemplate oldValue, DataTemplate newValue)  
        {  
            if (this.NullValueListBoxItem != null)  
            {  
                this.NullValueListBoxItem.ContentTemplate = newValue;  
                this.OnApplyTemplate();  
            }  
        }  
 
        protected virtual void OnSelectionChanged(object sender, SelectionChangedEventArgs e)  
        {  
            if (this.SelectedItem == this.NullValueListBoxItem)  
            {  
                this.SelectedItem = null;  
                this.SelectedValue = null;  
            }  
 
        }  
 
        /// <summary>  
        /// Prepares the specified element to display the specified item.  
        /// </summary>  
        /// <param name="element">Element used to display the specified item.</param>  
        /// <param name="item">Specified item.</param>  
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)  
        {  
            base.PrepareContainerForItemOverride(element, item);  
 
            var item2 = element as RadComboBoxItem;  
 
            if (item == null && item2 != null)  
            {  
                item2.Content = NullValueContent;  
                item2.ContentTemplate = NullValueContentTemplate;  
                NullValueListBoxItem = item2;  
            }  
        }  
 
        /// <summary>  
        /// Called when [null value content property changed].  
        /// </summary>  
        /// <param name="d">The d.</param>  
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>  
        private static void OnNullValueContentPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)  
        {  
            ((RadCustomComboBox)d).OnNullValueContentChanged(e.OldValue, e.NewValue);  
        }  
    } 


Thanks!

2 Answers, 1 is accepted

Sort by
0
Valeri Hristov
Telerik team
answered on 22 Feb 2010, 12:08 PM
Hello,

I will need some time to see what's wrong with your code, I will write back here when I have more information. Meanwhile, I would like to suggest using the ClearValue button in RadComboBox, that seems to do exactly what you need, except it is displayed like a button, not like a regular item. There is more information about this button at the end of the following article:
http://www.telerik.com/help/silverlight/radcombobox-features-selection.html

Sincerely yours,
Valeri Hristov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Valeri Hristov
Telerik team
answered on 05 Mar 2010, 09:13 AM
Hi,

I just wanted to share that RadComboBox will contain EmptyText and EmptySelectionBoxTemplate properties that will allow you to customize the control when it has no selection :)

All the best,
Valeri Hristov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
ComboBox
Asked by
Jose Jones
Top achievements
Rank 1
Answers by
Valeri Hristov
Telerik team
Share this question
or