LTU
LTU

Reputation: 205

WPF. If ListView empty, ListView dissappear

i have problem with ListView. If there are no items, its has to shown for example something like "No item" And I can do it, but if i do ListView disappear. I need that this text appear inside listview and listview header have to stay the same.

My listView style for empty list now is:

 <Style TargetType="{x:Type ListView}" >
            <Setter Property="BorderThickness" Value="2,2,0,0"/>
            <Setter Property="BorderBrush" Value="#FFFFFF"/>

            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Items.Count,
                    RelativeSource={RelativeSource Self}}"  Value="0">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>

                                <Border BorderThickness="1" BorderBrush="#FFFFFF"
                                        Background="#FFFFFF">
                                   <TextBlock> No items</TextBlock>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

Upvotes: 3

Views: 819

Answers (1)

Glen Thomas
Glen Thomas

Reputation: 10764

It would be best to move the border outside of the ListView template. Just lay it over the top and hide it when no items in ListView:

<Grid>
    <Grid.Resources>
        <converter:InverseBooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </Grid.Resources>
    <ListView x:Name="List">
    </ListView>
    <Border BorderThickness="1" BorderBrush="#FFFFFF" Background="#FFFFFF"
            Visibility="{Binding ElementName=List, Path=HasItems, Converter={StaticResource BooleanToVisibilityConverter}}">
        <TextBlock> No items</TextBlock>
    </Border>
</Grid>

Converter:

class InverseBooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((bool)value)
        {
            return Visibility.Collapsed;
        }

        return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Upvotes: 3

Related Questions