0

This is my ViewModel:

public ObservableCollection<Person> Persons;
class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
}

And i want to create menu with all this Persons object from my List.

So in XAML i just create this:

<Menu Name="menuPersons" DockPanel.Dock="Top" SnapsToDevicePixels="True">

</Menu>

Code behind

    MenuItem fileMenuItem = new MenuItem();
    fileMenuItem.Header = "Persons";
    menuPersons.Items.Add(fileMenuItem);

    foreach (Person item in Persons)
    {
          fileMenuItem.Items.Add(new MenuItem
          {
              Header = item.FirstName,
          });
    }

But as you can see this looks ugly so my question is is pure XAML way to do that and bind all my object with all its properties ?

Update

So the solution works fine for me but i only have one issue:

<Menu.ItemContainerStyle>
    <Style TargetType="{x:Type MenuItem}">
         <Setter Property="Command" Value="{Binding Command}"/>
    </Style>
</Menu.ItemContainerStyle>

I have Style that i am using but so i cannot use this Menu.ItemContainerStyle

How can i solve this ? (note that i have another several different Menus that i using with this Style but i only need this Command for this Menu)

Clemens
  • 123,504
  • 12
  • 155
  • 268
falukky
  • 1,099
  • 2
  • 14
  • 34

0 Answers0