WPF ContextMenu Словарьпривязки данных - PullRequest
4 голосов
/ 09 ноября 2010

Допустим, следующие определения классов.


    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>

Вопрос заключается в следующем: как сделать привязку данных ICommand для пункта меню DataProvider щелчком мыши и получить тип данных (тип enum) и поставщика данных (интерфейс 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 представит экземпляр этого командного класса как свойство.

1 Ответ

0 голосов
/ 11 мая 2011

Проблема была решена:

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