Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listbox Item Template for an empty list

I have a list box with an item template defined in XAML like this:

        <ListBox Name="listBoxDisruptions">

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
                        <TextBlock Text="{Binding text}" Foreground="Black" FontSize="29">Hello! some item</TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>             

        </ListBox>

Now what i want is to display a line of text in the center of the listbox in case the ItemSource for this listbox is empty.

Does XAML support some kind of no item template, ? something like this:

    <ListBox Name="listBoxDisruptions">

                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
                            <TextBlock Text="{Binding text}" Foreground="Black" FontSize="29">Hello! some item</TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate> 

<ListBox.NoItemTemplate>
<TextBlock Text="No Items to display"/>
</ListBox.NoItemTemplate>                   

            </ListBox>

So ?

like image 595
Ahmad Mushtaq Avatar asked Mar 17 '11 10:03

Ahmad Mushtaq


2 Answers

There might be a XAML way to do it using WPF-like techniques - Listbox Item Template for an empty list

However, in Overflow7 I got bored trying to make these work - so I used a slightly-hacky trick instead of adding an extra TextBlock to the page and then using:

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { 
            listBox1.ItemsSource = data; 

            data.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(data_CollectionChanged); 

        } 

        void data_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { 
            if (data.Count == 0) 
                textBlock1.Visibility = Visibility.Visible; 
            else 
                textBlock1.Visibility = Visibility.Collapsed; 
        } 

(Trick learnt from http://forums.create.msdn.com/forums/p/70755/431687.aspx)

like image 152
Stuart Avatar answered Nov 12 '22 16:11

Stuart


You could take the textblock outside of the listview, and then bind the visibility of the textblock to the count of the list your using for the listview, using a converter.

EDIT: Example as asked for:-

<ListView ItemsSource="{Binding MyItemSource}">
   <ListView.ItemTemplate>
     <DataTemplate>
       <Grid/>
     </DataTemplate>
   </ListView.ItemTemplate>
</ListView>
<TextBlock Text="Some text" Visibility="{Binding MyItemSource.Count, Converter={StaticResource CountToVisibilityConverter}}"/>

Declare the converter either in your page resources, or a resource dictionary in your app like so:

<converters:CountToVisibilityConverter x:Key="CountToVisibilityConverter" />

and then the converter could be:

public sealed class CountToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string culture)
    {
        if (value != null)
        {
            var i = (Int32)value;

            if (i > 0)
                return Visibility.Collapsed;
            else
                return Visibility.Visible;
        }
        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string culture)
    {
        return new NotImplementedException();
    }
}
like image 2
jack moseley Avatar answered Nov 12 '22 16:11

jack moseley