代码之家  ›  专栏  ›  技术社区  ›  Sergey Vyacheslavovich Brunov prodigitalson

WPF ContextMenu Dictionary<Key,List<Value>>数据绑定

  •  4
  • Sergey Vyacheslavovich Brunov prodigitalson  · 技术社区  · 14 年前

    假设以下类定义。

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

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

    问题是:如何使data provider菜单项的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 回复  |  直到 14 年前
        1
  •  0
  •   Sergey Vyacheslavovich Brunov prodigitalson    14 年前

    问题已经解决:

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