使用MVVM通过DataTemplate将ContextMenu添加到TabItem

时间:2014-12-02 11:39:07

标签: c# wpf mvvm contextmenu

我有一个带有DataTemplate的TabControl ..我已经尝试在TabControl上有一个ContextMenu,它有点工作,但仅限于所选项目 - 而不是我实际点击的标签页眉。我尝试将ContextMenu添加到TabItem标题中,菜单显示,但功能不再有效。

查看:

<TabControl VerticalAlignment="Center" HorizontalAlignment="Left" Margin="50,0,0,0" ItemsSource="{Binding Playlists}" >
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding PlaylistID}">
                <TextBlock.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Rename" FontWeight="ExtraLight" >
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click">
                                    <cmd:EventToCommand Command="{Binding MenuRenameCommand}" 
                                                    CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </MenuItem>
                        <MenuItem Header="Delete" FontWeight="ExtraLight" >
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click">
                                    <cmd:EventToCommand Command="{Binding MenuDeleteCommand}" 
                                                        CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </MenuItem>
                    </ContextMenu>
                </TextBlock.ContextMenu>
            </TextBlock>
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate></DataTemplate>
    </TabControl.ContentTemplate>

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <cmd:EventToCommand Command="{Binding SelectionChangedCommand}" 
                                CommandParameter="{Binding Path=SelectedItem, 
                                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabControl}}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TabControl>

视图模型:

public ObservableCollection<PlaylistModel> Playlists
{
    get { return _playlists; }
    set
    {
        if (value.Equals(_playlists)) return;
        _playlists = value;
        RaisePropertyChanged("Playlists");
    }
}

public RelayCommand<PlaylistModel> MenuRenameCommand { get; set; }

public RelayCommand<PlaylistModel> MenuDeleteCommand { get; set; }

...

public void Initialize()
{
    MenuRenameCommand = new RelayCommand<PlaylistModel>((e) =>
    {
        if (e == null) return;

        string newName = DialogInputWindow.Prompt("Enter a new name for the playlist", "Rename playlist");

        if (String.IsNullOrEmpty(newName) || String.IsNullOrWhiteSpace(newName) || !Mapp.Move(e.PlaylistID, newName))
        {
            DialogInputWindow.Message("Could not rename playlist. Does a playlist with that name already exist?", "Rename failed");
            return;
        }

        Playlists[Playlists.IndexOf(e)].PlaylistID = newName;
    });
}

我无法让这个工作,所以我希望你们能帮忙。

提前致谢。

0 个答案:

没有答案