Если я щелкну элемент в середине списка, я ожидаю, что все элементы, кроме 1, будут свернуты. Фактический результат таков, что осталось много предметов. Почему? Это вся программа.
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>