Reputation: 1456
I am trying to make a context menu show up a list of strings. The app contains a datagrid of people who can be Edited,Deleted and Added to a group. I have a class StudentGroup which are different groups a person can be added (One of the Members of StudentGroup is Name). ViewModel retrieves the full list of groups and puts it inside an Observable Collection. I am trying to make the context menu work such that whenever a user right clicks and hover over Add User to -> it opens a side menuitems that contains the Observable Collections's Name string.
This is what I have tried so far by looking up similar questions on StackOverflow, but it hasn't worked for me yet.
The XAML:
<DataGrid.ContextMenu>
<ContextMenu AllowDrop="True" ItemsSource="{Binding Entries}">
<MenuItem Header="Edit" />
<MenuItem Header="Delete"/>
<MenuItem Header="Add User to">
<MenuItem.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
XAML Code behind View:
private TitleViewMode TVM=new TitleViewMode;
public Welcome()
{
InitializeComponent();
_grid1.ContextMenu.DataContext = TVM;
}
ViewModel
class TitleViewModel
{
public ObservableCollection<StudentGroup> Entries {get;set;}
private List<StudentGroup> sg1 { get; set;}
public TitletViewModel()
{
sg1 = GetGroups();
Entries = new ObservableCollection<StudentGroup>(sg1);
}
}
Upvotes: 5
Views: 4055
Reputation: 30097
This should work
<MenuItem Header="Add User to" ItemsSource="{Binding Entries}">
<MenuItem.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding Name}"></MenuItem>
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
Upvotes: 4