mardi 22 avril 2014

c# - impossible de trouver directeurs FrameworkElement ou FrameworkContentElement pour l'élément cible - Stack Overflow


For the record, I've searched and seen this issue discussed many times, but I cannot get any of the discussed solutions to work in my case. This is also my first WPF project, so this is alot to take in all at once.


I have a TreeView with some nodes. Each of these nodes displays a ContextMenu, where the items displayed in the menu are dependent on what type of node it is. In Xaml, all of this looks like the following:


<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>

In code, the implementation of AttachDebuggerCommand looks like the following:


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); }
}
}

When I run this, I get the following error in the output window:


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')

What can I do to fix this? For starters, one thing that stands out to me is that the TargetElement is the command. It's supposed to be getting the value from the InstalledBuild, or whatever the type of the HierarchicalDataTemplate that's being applied is.


I've seen solutions mentioning something about injecting a virtual branch and various other things, but I'm having some trouble getting anything to work in my scenario.



For the record, I've searched and seen this issue discussed many times, but I cannot get any of the discussed solutions to work in my case. This is also my first WPF project, so this is alot to take in all at once.


I have a TreeView with some nodes. Each of these nodes displays a ContextMenu, where the items displayed in the menu are dependent on what type of node it is. In Xaml, all of this looks like the following:


<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>

In code, the implementation of AttachDebuggerCommand looks like the following:


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); }
}
}

When I run this, I get the following error in the output window:


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')

What can I do to fix this? For starters, one thing that stands out to me is that the TargetElement is the command. It's supposed to be getting the value from the InstalledBuild, or whatever the type of the HierarchicalDataTemplate that's being applied is.


I've seen solutions mentioning something about injecting a virtual branch and various other things, but I'm having some trouble getting anything to work in my scenario.


0 commentaires:

Enregistrer un commentaire