【问题标题】:WPF ContextMenu Dictionary<Key, List<Value>> databindingWPF ContextMenu Dictionary<Key, List<Value>> 数据绑定
【发布时间】:2010-11-09 08:13:43
【问题描述】:

假设以下类定义。


    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 上下文菜单如下:


    <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接口)。

更新 我想让一些命令类绑定到 MenuItems,例如:


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 会将此命令类的实例作为属性公开。

【问题讨论】:

    标签: wpf data-binding menu icommand


    【解决方案1】:

    问题已经解决了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多