WPF上下文菜单数据绑定中的字典<Key, List<Value>>。

4
假设以下类定义。

    public enum ContentType { Playlist, Audio, Video, Picture }

    public interface IDataProvider
    {
        string Name
        {
            get;
        }
    }

    public class ProviderList : List<IDataProvider>
    {
    }

    public class MainViewModel
    {
        public Dictionary<ContentType, ProviderList> ProvidersDictionary;

        public MainViewModel()
        {
            if (IsInDesignMode)
            {
            // Code runs in Blend --> create design time data.
            }
            else
            {
            // Code runs "for real"
                this.ProvidersDictionary = new Dictionary<ContentType, ProviderList>();
                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);
            }
        }
    }

因此,此ProvidersDictionary属性绑定到窗口上下文菜单,如下所示:


    <Window.ContextMenu>
        <ContextMenu ItemsSource="{Binding ProvidersDictionary}">
            <ContextMenu.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Value}">
                    <TextBlock Margin="1" Text="{Binding Key}" Height="20"/>

                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </ContextMenu.ItemTemplate>
        </ContextMenu>
    </Window.ContextMenu>

问题是:如何为DataProvider菜单项的单击事件创建ICommand数据绑定,并在命令的Execute方法中检索数据类型(枚举类型)和数据提供程序(IDataProvider接口)。
更新: 我想要将一些命令类绑定到菜单项,例如:

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
        }
    }
}

MainViewModel将此命令类的实例公开为属性。

1个回答

0

问题已解决:

<UserControl x:Class="DataProvidersView"
    ...

    <ContextMenu.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}">
        <Setter Property="Command" Value="{Binding Path=DataContext.DataProviderSwitchCommand,
                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type vw:DataProvidersView}}}" />
        <Setter Property="CommandParameter">
            <Setter.Value>
            <MultiBinding>
                <Binding Path="DataContext.Key"
                     RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}" />
                <Binding />
            </MultiBinding>
            </Setter.Value>
        </Setter>
        </Style>
    </ContextMenu.ItemContainerStyle>
</UserControl>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接