完全隐藏GridView(用作ListView's视图)中的元素

本文关键字:视图 元素 隐藏 GridView ListView 用作 | 更新日期: 2023-09-27 18:15:05

我有一个ListView和一个按钮。现在,当我按下按钮时,我希望只显示满足所需标准的元素。我正在使用项目可见性的转换器,但如果我返回Visibility.Hidden,项目仅仅是不可见的,但它们仍然占用空间。一个更好的选择是使用Visibility.Collapsed,它缩小项目并隐藏它们,但它们仍然占用一点空间。我需要他们完全离开ListView

下面是XAML代码(我知道它不是很干净,但由于我有一个宽监视器,它更容易有更少的行比列):

        <ListView x:Name="lvTereni1" Grid.Column="0" Grid.Row="0" dd:DragDrop.IsDropTarget="True" dd:DragDrop.DropHandler="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Width="120" Margin="5" OpacityMask="#CA000000" SelectionMode="Single" ItemsSource="{Binding Path=SveHale.Hala1}" GridViewColumnHeader.Click="lvTereni1_Click" MouseDoubleClick="ListView_MouseDoubleClick">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Glavna sala" Width="110">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Border Height="40" Width="80" CornerRadius="15">
                                    <Border.Visibility>
                                        <MultiBinding Converter="{StaticResource ShowHideConverter}">
                                            <MultiBinding.Bindings>
                                                <Binding Path="BZauzet" />
                                                <Binding Path="Stanje" />
                                                <Binding ElementName="btnSwap" Path="Text"></Binding>
                                            </MultiBinding.Bindings>
                                        </MultiBinding>
                                    </Border.Visibility>
                                    <Border.Background>
                                        <MultiBinding Converter="{StaticResource DragAndDropCoverter}">
                                            <MultiBinding.Bindings>
                                                <Binding ElementName="lvTimovi" Path="SelectedItem" />
                                                <Binding Path="Stanje"/>
                                                <Binding Path="DozvoljeniSportovi" />
                                                <Binding Path="BZauzet" />
                                            </MultiBinding.Bindings>
                                        </MultiBinding>
                                    </Border.Background>
                                    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Path=Sifra}">
                                        <TextBlock.Foreground>
                                            <MultiBinding Converter="{StaticResource DragAndDropTextCoverter}">
                                                <MultiBinding.Bindings>
                                                    <Binding Path="Stanje"/>
                                                    <Binding Path="BZauzet" />
                                                </MultiBinding.Bindings>
                                            </MultiBinding>
                                        </TextBlock.Foreground>
                                    </TextBlock>
                                </Border>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

ShowHideConverter是这里有趣的转换器(我知道我不应该将转换器绑定到相对于按钮文本的东西,但这不是一个大项目,按钮的文本块总是有两个状态,所以…):

class ShowHideConverter : IMultiValueConverter
{
    public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
    {
        bool zauzet = (bool)value[0];
        Teren._Stanje stanje = (Teren._Stanje)value[1];
        string ispis = (string)value[2];
        if (ispis.Equals("Prikaži sve"))
            if (zauzet || stanje == Teren._Stanje.Zatvoren)
                return Visibility.Collapsed;
        return Visibility.Visible;
    }
    public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

完全隐藏GridView(用作ListView's视图)中的元素

你可以在CollectionView中包装你的集合(SveHale.Hala1),并根据按钮状态使用Filter属性。例如,如果您的按钮状态绑定到SveHale.Hala1集合周围的FilterDataHala1Wrapper包装器属性:

public bool FilterData 
{
    set 
    {
        if (value) 
            Hala1Wrapper.Filter = itm => zauzet || ((Teren._Stanje)itm) == Teren._Stanje.Zatvoren;
        else 
            Hala1Wrapper.Filter = null;
    }
}

在XAML中,你必须绑定ListView ItemsSourceCollectionView包装器。

<ListView  ItemsSource="{Binding Path=SveHale.Hala1Wrapper}">