简体   繁体   English

将ContextMenu命令绑定到父视图模型RelayCommand

[英]Bind ContextMenu Command to parents viewmodel RelayCommand

I have a TvShowsViewModel (1) that contains an ObservableCollection of TvShowViewModel s (2). 我有一个TvShowsViewModel (1),其中包含一个ObservableCollectionTvShowViewModel (2)。 A TvShowViewModel contains an ObservableCollection of SeasonViewModel s (3). TvShowViewModel包含ObservableCollectionSeasonViewModel S(3)。

I have a TreeView , which has the TvShowsViewModel (1) as DataContext . 我有一个TreeView ,它具有TvShowsViewModel (1)作为DataContext The ItemSource of that TreeView binds to the ObservableCollection of TvShowViewModels (2). TreeViewItemSource绑定到TvShowViewModelsObservableCollection (2)。

The TreeView specifies a HierarchicalDataTemplate , which binds to the ObservableCollection of SeasonViewModels (3). TreeView指定一个HierarchicalDataTemplate ,该SeasonViewModels绑定到SeasonViewModelsObservableCollection (3)。

The HierarchicalDataTemplate contains a ContextMenu . HierarchicalDataTemplate包含一个ContextMenu

Now, the ContextMenu contains a Command which I want to bind to a RelayCommand in the TvShowsViewModel (1). 现在, ContextMenu包含了Command ,我想绑定到RelayCommandTvShowsViewModel (1)。

I tried all kinds of RelativeSource bindings, but nothing that leads to the solution. 我尝试了各种RelativeSource绑定,但是没有任何解决方案。 How should I specify the binding? 我应该如何指定绑定?

TvShowsViewModel (1) TvShowsViewModel(1)

public class TvShowsViewModel : ViewModelBase
{
    public RelayCommand ExcludeSeasonCommand { get; private set; }

    public ObservableCollection<TvShowViewModel> TvShows { get; private set; }

    public TvShowsViewModel(ITvShowsLibrary tvShowsLibrary)
    {
        TvShows = new ObservableCollection<TvShowViewModel>();

        ExcludeSeasonCommand = new RelayCommand(ExcludeSeasonCommandOnExecute, ExcludeSeasonCommandOnCanExecute);

    // Left out irrelevant code
    }
}

TvShowViewModel (2) TvShowViewModel(2)

public class TvShowViewModel : ViewModelBase, IFolderOnDisk
{
    public ObservableCollection<SeasonViewModel> Seasons
    {
        get { return _seasons; }
    }

    // Left out irrelevant code
}

SeasonViewModel (3) SeasonViewModel(3)

public class SeasonViewModel : ViewModelBase, IFolderOnDisk
{
    // Left out irrelevant code
}

The stripped user control (TreeView) 剥离的用户控件(TreeView)

<!-- Again, left out a lot of irrelevant parts -->
<TreeView ItemsSource="{Binding TvShows}">

    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate DataType="tvShows:TvShowViewModel" ItemsSource="{Binding Seasons}">
            <TextBlock Text="{Binding Name}" />

            <HierarchicalDataTemplate.ItemTemplate>
                <HierarchicalDataTemplate>
                    <TextBlock Text="{Binding Name}">
                        <TextBlock.ContextMenu>
                            <ContextMenu>
                                <MenuItem
                                    Header="Exclude season"
                     <!-- This is where I need your help, how should I configure the binding? -->
                                    Command="{Binding Path=DataContext.ExcludeSeasonCommand, RelativeSource={RelativeSource AncestorType=TreeView}}" />
                            </ContextMenu>
                        </TextBlock.ContextMenu>
                    </TextBlock>
                </HierarchicalDataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

The binding, which I can't figure out 绑定,我不知道

Finally, after many many many google searches I ran into the solution 最后,经过许多Google搜索之后,我遇到了解决方案

<MenuItem
    Header="Exclude season"
    Command="{Binding DataContext.ExcludeSeasonCommand, Source={x:Reference _tvShowsTreeView}}" />

Because the HierarchicalDataTemplate does not appear in the visual tree, there is not "relative" source... 因为HierarchicalDataTemplate没有出现在视觉树中,所以没有“相对”源...

I hope this helps somebody else who's pulling his/her hair out... 我希望这对其他正在拔头发的人有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM