3

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>

1 Answer 1

3

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.