0

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;}          
   }
pnuts
  • 58,317
  • 11
  • 87
  • 139
DotNetSpartan
  • 931
  • 4
  • 20
  • 41
  • There is no 3 line solution to your problem, you could: 1) Create own Panel that will add 'Create book` at the bottom 2) Create own IEnumerable deriving from Observable Collection, that will return your Items + when collection is out return static item 'Create book' 3) You could try to make custom style for last item in MenuItem->Childs ( you could find if is last by `{Binding RelativeSource={RelativeSource PreviousData}}` is null, and in this style you would add normal menu item + button 'Create book' 4) You could try to write your own MenuItem, that under panel with childrens will al – sTrenat Jul 25 '18 at 07:32
  • ways add 'Create book' menu item – sTrenat Jul 25 '18 at 07:32

0 Answers0