вы можете использовать селектор DataTemplate для этого.
http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx
ОБНОВЛЕНИЕ 1
Код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace EmptyRowsTemplate
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += (o, e) =>
{
this.l.ItemsSource = new List<string>(3)
{
"A",
null,
"B"
};
};
}
}
public class TemplateManager : DependencyObject
{
public static readonly DependencyProperty BlankDataTemplateProperty =
DependencyProperty.RegisterAttached("BlankDataTemplate",
typeof(DataTemplate),
typeof(TemplateManager),
new PropertyMetadata(new PropertyChangedCallback((o, e) =>
{
((ItemsControl)o).ItemTemplateSelector = new BlankDataTemplateSelector();
})));
public static void SetBlankDataTemplate(DependencyObject o, DataTemplate e)
{
o.SetValue(TemplateManager.BlankDataTemplateProperty, e);
}
public static DataTemplate GetBlankDataTemplate(DependencyObject o)
{
return (DataTemplate)o.GetValue(TemplateManager.BlankDataTemplateProperty);
}
private class BlankDataTemplateSelector : DataTemplateSelector
{
public BlankDataTemplateSelector()
: base()
{
}
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
ItemsControl itemControl =
(ItemsControl)ItemsControl.ItemsControlFromItemContainer(ItemsControl.ContainerFromElement(null, container));
if (item == null)
{
return TemplateManager.GetBlankDataTemplate(itemControl);
}
else
{
return base.SelectTemplate(item, container);
}
}
}
}
}
Markup
<Window x:Class="EmptyRowsTemplate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:EmptyRowsTemplate"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox x:Name="l">
<local:TemplateManager.BlankDataTemplate>
<DataTemplate>
<Button Background="Red">No Data!</Button>
</DataTemplate>
</local:TemplateManager.BlankDataTemplate>
</ListBox>
</Grid>
</Window>