私は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;
}
}
ご協力ありがとう御座います!