无法为目标元素找到管理FrameworkElement或FrameworkContentElement

时间:2014-04-04 17:54:59

标签: c# wpf wpf-controls

为了记录,我已经多次搜索并看到了这个问题,但我无法让任何讨论过的解决方案适用于我的情况。这也是我的第一个WPF项目,所以这很可能会同时进行。

我有一些带有一些节点的TreeView。这些节点中的每一个都显示一个ContextMenu,其中菜单中显示的项目取决于它是什么类型的节点。在Xaml中,所有这些都如下所示:

<TreeView
    Name="BuildExplorerTreeView" Height="Auto"
    Background="{DynamicResource VsBrush.ToolWindowBackground}"
    Foreground="{DynamicResource VsBrush.WindowText}">
  <TreeView.Resources>
    <!-- Create a menu item, and map the Command to a custom ICommand implementation --> 
    <MenuItem x:Key="AttachDebuggerMenuItem" Header="Attach debugger">
      <MenuItem.Command>
        <commands:AttachDebuggerCommand
            AutoAttachToCurrentChildren="{Binding Path=AutoAttachToCurrentChildren}" 
            AutoAttachToFutureChildren="{Binding Path=AutoAttachToFutureChildren}" 
            Processes="{Binding Path=Processes}"/>
      </MenuItem.Command>
    </MenuItem>

    <!-- Create a Context Menu with one menu item -->
    <ContextMenu x:Key="TreeViewContextMenu">
      <StaticResourceExtension ResourceKey="AttachDebuggerMenuItem"/>
    </ContextMenu>

    <!-- Use this context menu in the DataTemplate for a specific node type -->
    <HierarchicalDataTemplate DataType="{x:Type buildExplorer:InstalledBuild}" ItemsSource="{Binding Processes}">
      <StackPanel Orientation="Horizontal" ContextMenu="{StaticResource TreeViewContextMenu}">
        <Image Style="{StaticResource IconImageStyle}"/>
        <TextBlock Text="{Binding DisplayText}" />
      </StackPanel>
    </HierarchicalDataTemplate>
  </TreeView.Resources>
</TreeView>

在代码中,AttachDebuggerCommand的实现如下所示:

public class AttachDebuggerCommand : DependencyObject, ICommand {
  public static readonly DependencyProperty ProcessesProperty
      = DependencyProperty.Register("Processes", 
                                    typeof(IList<MyProcess>), 
                                    typeof(AttachDebuggerCommand));

  public static readonly DependencyProperty AutoAttachToCurrentChildrenProperty
      = DependencyProperty.Register("AutoAttachToCurrentChildren", 
                                    typeof(bool), 
                                    typeof(AttachDebuggerCommand));

  public static readonly DependencyProperty AutoAttachToFutureChildrenProperty
      = DependencyProperty.Register("AutoAttachToFutureChildren", 
                                    typeof(bool), 
                                    typeof(AttachDebuggerCommand));

  public AttachDebuggerCommand() {
  }

  public bool CanExecute(object parameter) {
    return true;
  }

  public event EventHandler CanExecuteChanged {
    add { }
    remove { }
  }

  public void Execute(object parameter) {
  }

  public IList<MyProcess> Processes {
    get { return (IList<MyProcess>)GetValue(ProcessesProperty); }
    set { SetValue(ProcessesProperty, value); }
  }

  public bool AutoAttachToCurrentChildren {
    get { return (bool)GetValue(AutoAttachToCurrentChildrenProperty); }
    set { SetValue(AutoAttachToCurrentChildrenProperty, value); }
  }

  public bool AutoAttachToFutureChildren {
    get { return (bool)GetValue(AutoAttachToFutureChildrenProperty); }
    set { SetValue(AutoAttachToFutureChildrenProperty, value); }
  }
}

当我运行它时,我在输出窗口中收到以下错误:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=AutoAttachToCurrentChildren; DataItem=null; target element is 'AttachDebuggerCommand' (HashCode=59189449); target property is 'AutoAttachToCurrentChildren' (type 'Boolean')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=AutoAttachToFutureChildren; DataItem=null; target element is 'AttachDebuggerCommand' (HashCode=59189449); target property is 'AutoAttachToFutureChildren' (type 'Boolean')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Processes; DataItem=null; target element is 'AttachDebuggerCommand' (HashCode=59189449); target property is 'Processes' (type 'IList`1')

我该怎么做才能解决这个问题?对于初学者来说,有一点让我感到惊讶的是TargetElement就是命令。它应该是从InstalledBuild获取值,或者是应用的HierarchicalDataTemplate的类型。

我已经看过解决方案,提到注入虚拟分支和其他各种事情,但是在我的场景中让任何事情都工作时我遇到了一些麻烦。

0 个答案:

没有答案