RadListBox EmptyDataTemplate Behavior

2 posts, 1 answers
  1. David
    David avatar
    1 posts
    Member since:
    May 2013

    Posted 27 Jun 2013 Link to this post

    Attempting to create a behavior for the Silverlight RadListBox which implements the EmptyDataTemplate seen in the ASP.NET RadListBox. So far, I have the behavior created and declared in XAML, but one problem is that RadListBox.Items does not have a CollectionChanged event. So, I decided to use the SelectionChanged in order to asynchronously detect changes to the ListBox. Not the best choice, but just testing the waters.

    Here is the behavior implementation:

    public class EmptyListBoxBehavior : Behavior<RadListBox>
        public DataTemplate EmptyDataTemplate { get; set; }
        private ContentPresenter contentPresenter = new ContentPresenter();
        protected override void OnAttached()
            this.AssociatedObject.LayoutUpdated += AssociatedObject_LayoutUpdated;
        private void AssociatedObject_LayoutUpdated(object sender, EventArgs e)
            this.AssociatedObject.LayoutUpdated -= AssociatedObject_LayoutUpdated;
            this.AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged; // HACK RadListBox.Items.CollectionChanged does not exist
        void AssociatedObject_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
        private void LoadEmptyDataTemplate()
            contentPresenter.IsHitTestVisible = false;
            contentPresenter.DataContext = this;
            contentPresenter.ContentTemplate = this.EmptyDataTemplate;
            // Need to find which type of child is needed here (must inherit from DependencyObject)
            var child = this.AssociatedObject.ChildrenOfType<DependencyObject>().ToList();
            // TODO Not sure where to add the DataTemplate?
            // In RadGridView, you can do: RadGridView.ChildrenOfType<Grid>().First() and add it there by,
            // Grid.Children.Add(contentPresenter)
        private void SetEmptyDataTemplateVisibility()
            contentPresenter.Visibility = this.AssociatedObject.Items.Count == 0
                ? Visibility.Visible
                : Visibility.Collapsed;

    The last piece here is determining how/where to add the ContentPresenter to the visual elements of the RadListBox.
  2. Answer
    Vladi avatar
    749 posts

    Posted 02 Jul 2013 Link to this post


    In the RadListBox control for Silverlight there isn't a CollectionChanged on its items. If you have bound the ItemsSource property of the control to a ObservableCollection you could use its CollectionChanged instead. In order to edit the template of the items you should create a custom Template and set it to the ItemTemplate of the ListBox control.

    Hope this is helpful.

    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top