【问题标题】:Binding command to a MenuItem using HierarchicalDataTemplate使用 HierarchicalDataTemplate 将命令绑定到 MenuItem
【发布时间】:2011-07-15 22:24:44
【问题描述】:

我目前有一个使用分层数据模板显示数据的树视图,当用户单击“删除”按钮时,它会删除选定的节点。这按预期工作。

但是,我没有让用户单击按钮,而是尝试通过右键单击节点并选择适当的菜单项来执行命令。

事实证明这要困难得多,因为它是由节点的 ViewModel(它不知道任何关于 View)而不是 View 的相应 ViewModel 拾取的。

有没有办法将控制权交给 View 的 ViewModel?

这里是删除按钮的代码:

查看:

        <Button Content="Remove" Grid.Row="2" Height="23" VerticalAlignment="Top" Name="removeButton" 
                Width="75"  Margin="5,20,5,0"  Command="{Binding Path=RemoveCommand}" />

视图模型:

public RelayCommand RemoveCommand
        {
            get
            {
                if (_removeCommand == null)
                {
                    _removeCommand = new RelayCommand(
                        () => this.Remove()
                        );
                }
                return _removeCommand;
            }
        }

        public void Remove()
        {
            _organLocationTree2.RemoveOrganLocations(ProjectOrganLocationView.GetExtendedTreeView().SelectedItems);
            ProjectOrganLocationView.GetExtendedTreeView().SelectedItems.Clear();

            base.RaisePropertyChanged("DestOrganTree");
        }

以及菜单项的 XAML:

            <local:ExtendedTreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding SubOrganLocations}">
                    <TextBlock Text="{Binding OrganName}" >
            <TextBlock.ContextMenu>
                <ContextMenu>
                    <MenuItem Header ="Add SubNode" Command="{Binding Path=MenuItem_Add}"></MenuItem>
                    <MenuItem Header ="Remove Node" Command="{Binding Path=RemoveCommand}"></MenuItem>
                    <MenuItem Header ="Edit Node" Command="{Binding Path=ProjMenuItem_Edit}"
                              CommandParameter="{Binding DestOrganTree, Path=Selected.OrganName}"></MenuItem>
                </ContextMenu>
            </TextBlock.ContextMenu>
                    </TextBlock>
                </HierarchicalDataTemplate>
            </local:ExtendedTreeView.ItemTemplate>
        </local:ExtendedTreeView>

我试图在节点的 ViewModel 中实现一个 Remove 命令,但由于它对 View 一无所知,所以很快就变得非常混乱。

【问题讨论】:

    标签: wpf data-binding mvvm


    【解决方案1】:

    好吧,我发现我的错误,我将上下文菜单绑定到树的节点而不是树本身。我将上下文菜单移到了声明之外,现在它可以按预期工作了。

    这是我为遇到此问题的其他人更新的 xaml:

     <local:ExtendedTreeView.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header ="Add SubNode" Command="{Binding Path=MenuItem_Add}"></MenuItem>
                                <MenuItem Header ="Remove Node" Command="{Binding Path=RemoveCommand}"></MenuItem>
                                <MenuItem Header ="Edit Node" Command="{Binding Path=ProjMenuItem_Edit}"
                                          CommandParameter="{Binding DestOrganTree, Path=Selected.OrganName}"></MenuItem>
                            </ContextMenu>                        
                        </local:ExtendedTreeView.ContextMenu>
    
                        <local:ExtendedTreeView.ItemTemplate>
                            <HierarchicalDataTemplate ItemsSource="{Binding SubOrganLocations}">
                                <TextBlock Text="{Binding OrganName}" >
                                </TextBlock>
                            </HierarchicalDataTemplate>
                        </local:ExtendedTreeView.ItemTemplate>
    

    【讨论】:

    • 是的,这对我也更有效。但我希望在树视图的每个级别上都有不同的上下文项。我认为这个解决方案不能解决这个问题...#still_looking...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多