分组列表框和SelectionChanged
本文关键字:SelectionChanged 列表 | 更新日期: 2023-09-27 18:19:38
我是WPF的新手…:)
我需要ListBox来显示分组项目,这很好。
<ListBox Width="120" Loaded="ListBox_Loaded" SelectionChanged="ListBox_SelectionChanged" >
<ListBox.GroupStyle>
<GroupStyle />
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding Items}" BorderThickness="0" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
但是在*ListBox_SelectionChanged*SelectedIndex中仍然是-1,并且SelectedItems集合也是空的。
下面是一段代码:
public ICollectionView Groups()
{
List<Groups> groups = new List<AC.Groups>();
groups.Add(new AC.Groups { Items = Properties.Settings.Default.Worker, Name="Worker" });
groups.Add(new AC.Groups { Items = Properties.Settings.Default.Flow, Name = "Flow" });
ICollectionView groups = CollectionViewSource.GetDefaultView(groups);
groups.GroupDescriptions.Add(new PropertyGroupDescription("Name"));
return groups;
}
private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
(sender as ListBox).ItemsSource = Groups();
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show((sender as ListBox).SelectedIndex.ToString());
}
class Groups
{
public System.Collections.Specialized.StringCollection Items { get; set; }
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
谢谢你的帮助!
<DataTemplate>
<ListBox ItemsSource="{Binding Items}" BorderThickness="1" SelectionChanged="ListBox_SelectionChanged" />
</DataTemplate>
错误放置的事件处理程序…:)