【问题标题】:WPF MVVM bind command to treeview context menuWPF MVVM 绑定命令到树视图上下文菜单
【发布时间】:2017-08-19 06:50:20
【问题描述】:

我有一个树形视图,我想要一个右键单击菜单。单击菜单项时,应使用该项目调用命令。我尝试了不同的解决方案,但不断收到如下错误。

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression

我的树视图如下

   <TreeView ItemsSource="{Binding Buildings}" Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" Grid.RowSpan="4">
                    <TreeView.Resources>
                        <HierarchicalDataTemplate  ItemsSource="{Binding Gates}" DataType="{x:Type local:Floor}">

                            <TextBlock Text="{Binding Name}" />
                        </HierarchicalDataTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding Floors}" DataType="{x:Type local:Building}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Name}" >
                                    <TextBlock.ContextMenu>
                                        <ContextMenu>
                                            <ContextMenu.ItemContainerStyle>
                                                <Style TargetType="MenuItem">
                                                    <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.EditCmd}"/>
                                                </Style>
                                            </ContextMenu.ItemContainerStyle>
                                        </ContextMenu>
                                    </TextBlock.ContextMenu>
                                </TextBlock>
                            </StackPanel>
                        </HierarchicalDataTemplate>
                        <DataTemplate DataType="{x:Type local:Gate}">
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </TreeView.Resources>
                </TreeView>

也在视图模型中

   private ICommand editCmd;
    public ICommand EditCmd
    {
        get
        {
            if (editCmd == null)
                editCmd = new DelegateCommand(EditObjectFunction);
            return editCmd;
        }
    }

我尝试了不同的解决方案,例如This,但无法解决问题

【问题讨论】:

标签: c# wpf xaml mvvm treeview


【解决方案1】:

UserControl 不是MenuItem 的视觉祖先,因为ContextMenu 驻留在它自己的元素树中。

但是您可以将TextBlockTag 属性绑定到UserControl,然后将MenuItemCommand 属性绑定到ContextMenuPlacementTarget 属性:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Name}" Tag="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}">
        <TextBlock.ContextMenu>
            <ContextMenu>
                <ContextMenu.ItemContainerStyle>
                    <Style TargetType="MenuItem">
                        <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Tag.DataContext.EditCmd}"/>
                    </Style>
                </ContextMenu.ItemContainerStyle>
            </ContextMenu>
        </TextBlock.ContextMenu>
    </TextBlock>
</StackPanel>

【讨论】:

  • 这是我在上述解决方案中遇到的错误。System.Windows.Data 错误:4:无法找到用于绑定的源,参考 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl' ,祖先级别='1''。 BindingExpression:(无路径);数据项=空;目标元素是'TreeView'(名称='');目标属性是“标签”(类型“对象”)
  • TreeView 真的位于 UserControl 内吗?我对此表示怀疑。请发布您的完整 XAML。
猜你喜欢
  • 1970-01-01
  • 2011-04-19
  • 1970-01-01
  • 1970-01-01
  • 2011-09-13
  • 1970-01-01
  • 2010-11-28
  • 2020-05-08
  • 2012-11-06
相关资源
最近更新 更多