Si hago clic en un elemento en el medio de la lista, espero que todos los elementos excepto uno se colapsen. El resultado real es que quedan muchos elementos. ¿Por qué? Este es el programa completo.¿Por qué no se colapsan mis listboxitems?
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication2
{
public partial class MainWindow : Window
{
public class obj { }
public MainWindow()
{
InitializeComponent();
List<obj> objList = new List<obj>();
for (int i = 0; i < 30; i++) objList.Add(new obj());
lb.ItemsSource = objList;
}
private void lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox lb = sender as ListBox;
for (int i = 0; i < lb.Items.Count; i++)
{
ListBoxItem tmp = (ListBoxItem)(lb.ItemContainerGenerator.ContainerFromItem(lb.Items[i]));
if (tmp != null)
{
if (tmp.IsSelected)
tmp.Visibility = System.Windows.Visibility.Visible;
else
tmp.Visibility = System.Windows.Visibility.Collapsed;
}
}
}
}
}
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
>
<Grid>
<ListBox Name="lb" SelectionChanged="lb_SelectionChanged" IsSynchronizedWithCurrentItem="True" >
<ListBox.ItemTemplate >
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Name="tb1" Text="whatever"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Gran respuesta gracias. – 0x4f3759df