Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding an observable collection to a context menu
    text
    copied!<p>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. </p> <p>This is what I have tried so far by looking up similar questions on StackOverflow, but it hasn't worked for me yet. </p> <p>The XAML:</p> <pre><code>&lt;DataGrid.ContextMenu&gt; &lt;ContextMenu AllowDrop="True" ItemsSource="{Binding Entries}"&gt; &lt;MenuItem Header="Edit" /&gt; &lt;MenuItem Header="Delete"/&gt; &lt;MenuItem Header="Add User to"&gt; &lt;MenuItem.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Name}" /&gt; &lt;/DataTemplate&gt; &lt;/MenuItem.ItemTemplate&gt; &lt;/MenuItem&gt; &lt;/ContextMenu&gt; &lt;/DataGrid.ContextMenu&gt; </code></pre> <p>XAML Code behind View:</p> <pre><code>private TitleViewMode TVM=new TitleViewMode; public Welcome() { InitializeComponent(); _grid1.ContextMenu.DataContext = TVM; } </code></pre> <p>ViewModel</p> <pre><code>class TitleViewModel { public ObservableCollection&lt;StudentGroup&gt; Entries {get;set;} private List&lt;StudentGroup&gt; sg1 { get; set;} public TitletViewModel() { sg1 = GetGroups(); Entries = new ObservableCollection&lt;StudentGroup&gt;(sg1); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload