Skip to content

Instantly share code, notes, and snippets.

@iburlakov
Created July 17, 2013 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iburlakov/6022338 to your computer and use it in GitHub Desktop.
Save iburlakov/6022338 to your computer and use it in GitHub Desktop.
How to show custom template for ListView when it has no items to show.
<Window x:Class="TabControlExp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button Name="addItemButton" Content="add to the end" />
<Button Name="removeItemButton" Content="remove first" />
</StackPanel>
<ListView Grid.Row="1" ItemsSource="{Binding Items, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}">
<ListView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Green" BorderThickness="1">
<TextBlock Text="{Binding Name}" />
</Border>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Style>
<Style TargetType="ListView">
<Style.Triggers>
<Trigger Property="HasItems" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListView">
<Border BorderBrush="Red" BorderThickness="1">
<TextBlock Text="There are no items in the list" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListView.Style>
</ListView>
</Grid>
</Window>
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;
using System.Collections.ObjectModel;
namespace TabControlExp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Items = new ObservableCollection<ItemClass>();
addItemButton.Click += (sender, e) =>
{
this.Items.Add(new ItemClass(this.Items.Count));
};
removeItemButton.Click += (sender, e) =>
{
if (this.Items.Count > 0)
{
this.Items.RemoveAt(0);
}
};
}
public ObservableCollection<ItemClass> Items { get; set; }
}
public class ItemClass
{
public string Name { get; set; }
public ItemClass(int num)
{
this.Name = string.Format("Item number {0}", num);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment