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

RadListBox EmptyDataTemplate Behavior

1 Answer 47 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 27 Jun 2013, 04:12 PM
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()
    {
        base.OnAttached();
 
        LoadEmptyDataTemplate();
        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
        SetEmptyDataTemplateVisibility();
    }
 
    void AssociatedObject_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
    {
        SetEmptyDataTemplateVisibility();
    }
 
    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.

1 Answer, 1 is accepted

Sort by
0
Accepted
Vladi
Telerik team
answered on 02 Jul 2013, 09:00 AM
Hello,

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.

Regards,
Vladi
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
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 >>
Tags
ListBox
Asked by
David
Top achievements
Rank 1
Answers by
Vladi
Telerik team
Share this question
or