I have time trying to dynamically associate ContextMenu with a DataGrid. The DataGrid is tied to a list of objects that works fine:
<DataGrid ItemsSource="{Binding DataGridItems}">
For each DataGridItem, I have a list of MenuItems set to a property that I would like to use to bind to ContextMenu. I tried the following, but I think I missed something:
<DataGrid ItemsSource="{Binding DataGridItems}">
<DataGrid.ContextMenu>
<ContextMenu ItemsSource="{Binding ContextMenuItems}" >
<MenuItem Header="{Binding Name}" Command="{Binding OnClick}"></MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid>
public class DataGridItem
{
public ObservableCollection<ContextMenuItem> ContextMenuItems
{
get { return _contextMenuItems; }
}
}
Any help or guidance would be greatly appreciated.
Exist source
share