Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF ContextMenu Dictionary<Key, List<Value>> databinding
    text
    copied!<p>Assume the following class definitions.</p> <pre><code> public enum ContentType { Playlist, Audio, Video, Picture } public interface IDataProvider { string Name { get; } } public class ProviderList : List&lt;IDataProvider&gt; { } public class MainViewModel { public Dictionary&lt;ContentType, ProviderList&gt; ProvidersDictionary; public MainViewModel() { if (IsInDesignMode) { // Code runs in Blend --&gt; create design time data. } else { // Code runs "for real" this.ProvidersDictionary = new Dictionary&lt;ContentType, ProviderList&gt;(); ProviderList providerList = new ProviderList(); providerList.Add(new DataProvider()); this.ProvidersDictionary.Add(ContentType.Audio, providerList); providerList = new ProviderList(providerList); providerList.Add(new DataProvider()); this.ProvidersDictionary.Add(ContentType.Video, providerList); } } } </code></pre> <p>So, this ProvidersDictionary property is bound to Window context menu as follows:</p> <pre><code> &lt;Window.ContextMenu&gt; &lt;ContextMenu ItemsSource="{Binding ProvidersDictionary}"&gt; &lt;ContextMenu.ItemTemplate&gt; &lt;HierarchicalDataTemplate ItemsSource="{Binding Value}"&gt; &lt;TextBlock Margin="1" Text="{Binding Key}" Height="20"/&gt; &lt;HierarchicalDataTemplate.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Name}" /&gt; &lt;/DataTemplate&gt; &lt;/HierarchicalDataTemplate.ItemTemplate&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/ContextMenu.ItemTemplate&gt; &lt;/ContextMenu&gt; &lt;/Window.ContextMenu&gt; </code></pre> <p>The question is: how to make ICommand databinding for the DataProvider menu item click and to retrieve data type (enum type) and data provider (IDataProvider interface) within the command' Execute method.</p> <p><img src="https://i.stack.imgur.com/GKf9y.png" alt=""></p> <p><strong>Update</strong> I want to have some command class to be bound to MenuItems like:</p> <pre><code> class DataProviderMenuSelectCommand : ICommand { #region ICommand Members public void Execute(object parameter) { ContentTypeProviderPair contentProviderPair = parameter as ContentTypeProviderPair; if (contentProviderPair != null) { // contentProviderPair.Type property - ContentType // contentProviderPair.Provider property - IProvider } } } </code></pre> <p>MainViewModel will expose instance of this command class as a property.</p>
 

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