WPF。如果ListView为空,ListView消失

WPF. If ListView empty, ListView dissappear

我对 ListView 有疑问。如果没有项目,它必须显示例如 "No item" 之类的东西,我可以做到,但如果我这样做,ListView 就会消失。我需要此文本出现在列表视图中,并且列表视图 header 必须保持不变。

我的空列表的 listView 样式现在是:

 <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>

最好将边框移到 ListView 模板之外。只需将它放在顶部并在 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>

转换器:

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();
    }
}