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:
The last piece here is determining how/where to add the ContentPresenter to the visual elements of the RadListBox.
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.