When binding and setting Visibility to Collapsed the item's empty space still remains. I have tested with many examples. Simple one is here
<ListBox x:Name="HorizontalListBox"> |
<ListBox.ItemsPanel> |
<ItemsPanelTemplate> |
<telerik:RadCarouselPanel/> |
</ItemsPanelTemplate> |
</ListBox.ItemsPanel> |
<ListBox.ItemTemplate> |
<DataTemplate> |
<TextBlock Text="{Binding Name}" Visibility="{Binding Visible}" /> |
</DataTemplate> |
</ListBox.ItemTemplate> |
</ListBox> |
<Button Height="23" Width="70" Content="lol" Click="Button_Click" /> |
public class Person : INotifyPropertyChanged |
{ |
private int age; |
private String name; |
public Person(String name, int age) |
{ |
this.name = name; |
this.age = age; |
} |
public int Age |
{ |
get { return this.age; } |
set { this.age = value; } |
} |
public String Name |
{ |
get { return this.name; } |
set { this.name = value; } |
} |
Visibility _visible = Visibility.Visible; |
public Visibility Visible |
{ |
get { return _visible; } |
set { _visible = value; OnPropertyChanged("Visible"); } |
} |
#region INotifyPropertyChanged Members |
public event PropertyChangedEventHandler PropertyChanged; |
protected void OnPropertyChanged(string pname) |
{ |
if (PropertyChanged != null) |
PropertyChanged(this, new PropertyChangedEventArgs(pname)); |
} |
#endregion |
} |
HorizontalListBox.ItemsSource = this.CreateItemSource(); // call from anywhere, like constructor |
ObservableCollection<Person> list = new ObservableCollection<Person>(); |
private ObservableCollection<Person> CreateItemSource() |
{ |
list.Add(new Person("George", 15)); |
list.Add(new Person("Rashko", 55)); |
list.Add(new Person("Britney", 60)); |
list.Add(new Person("Joe", 32)); |
list.Add(new Person("Vader", 34)); |
list.Add(new Person("Luke", 16)); |
list.Add(new Person("Jimmy", 105)); |
list.Add(new Person("Batman", 45)); |
list.Add(new Person("Butters", 9)); |
list.Add(new Person("Cartman", 9)); |
list.Add(new Person("Bender", 150)); |
return list; |
} |
private void Button_Click(object sender, RoutedEventArgs e) |
{ |
list[3].Visible = System.Windows.Visibility.Collapsed; |
} |
Now, when you click the button you can see the 4th item got invisible but still taking the space. However if you remove ListBox.ItemsPanel tagand then check again it will be collapsed as it should.
I have checked it with few examples and all are same, is there anyway to do that except removing the item from collection ?