I've a menu item named 'Authors' whose ItemsSource is bound to an ObservableCollection called CollectionOfAuthors. Clicking on Authors opens a list of author names. For each author name, list of Books are displayed. What modifications i need to make to display a static menu item 'Create new book' along with list of other books under each AuthorName.
Like This: Author-->AuthorName1-->Create new book, Book 1, Book 2, Book 3
Here's my existing code:
MainWindow.xaml
<MenuItem Header="Authors" ItemsSource="{Binding CollectionOfAuthors}">
<MenuItem.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Books}">
<TextBlock Text="{Binding AuthorName}"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding BookName}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
MainWindowViewModel.cs
private ObservableCollection<Author> _collectionOfAuthors;
public ObservableCollection<Author> CollectionOfAuthors
{
get { return _collectionOfAuthors; }
set { SetProperty(ref _collectionOfAuthors, value); }
}
public class Author
{
public string AuthorName {get; set;}
private ObservableCollection<BookDetails> _Books;
public ObservableCollection<BookDetails> Books
{
get { return _Books; }
set { SetProperty(ref _Books, value); }
}
}
public class BookDetails
{
public string BookName{get; set;}
}