我有一个ListView
和一个按钮。现在,当我按下按钮时,我只想显示满足所需条件的元素。我在项目的可见性上使用了转换器,但如果我退回Visibility.Hidden
项目只是不可见的,但它们仍然占用空间。更好的选择是使用Visibility.Collapsed
which 缩小项目并隐藏它们,但它们仍然占用一些空间。我需要他们彻底离开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();
}
}