WPF.如果ListView为空,则ListView消失



我的ListView有问题。如果没有项目,它必须显示例如" no item"我可以这样做,但如果我做ListView消失。我需要这个文本出现在listview和listview头必须保持不变。

空列表的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();
    }
}

相关内容

  • 没有找到相关文章

最新更新