0

I've been doing a lot of research and I don't understand exaclty how to properly bind an ICommand to my TreeView item.

I have a VM that holds a Data Object.

This data object holds an ObservableCollection object of TreeNode item objects.

This TreeNode Item has properties such as Tag, IsSelected, Header and even a ContextMenu.

However I can't figure out how to bind an ICommand to a single TreeView item.

Here is my XAML of my TreeView

 <TreeView Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="1" Grid.RowSpan="5" x:Name="TestPlanTreeView" ItemsSource="{Binding Data.TestPlanCollection}" dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True"> <TreeView.ItemContainerStyle> <Style TargetType="TreeViewItem"> <Setter Property="Focusable" Value="{Binding Focusable, Mode=TwoWay}"/> <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/> <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="FontStyle" Value="Italic"/> </Trigger> </Style.Triggers> </Style> </TreeView.ItemContainerStyle> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Children}"> <TextBlock Text="{Binding Header}" Margin="2"> <TextBlock.ContextMenu> <ContextMenu ItemsSource="{Binding MenuCollection}"> <ContextMenu.ItemContainerStyle> <Style TargetType="MenuItem"> <Setter Property="Header" Value="{Binding Header}"/> <Setter Property="Command" Value="{Binding Command}"/> <Setter Property="CommandParameter" Value="{Binding CommandParameter}"/> </Style> </ContextMenu.ItemContainerStyle> </ContextMenu> </TextBlock.ContextMenu> </TextBlock> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> 
2
  • "I can't figure out how to bind an ICommand to a single TreeViewItem" -- what specifically are you having trouble with? Your code appears to bind a Command property value to your MenuItem.Command property. Is that not what you want to do? Does it not work? Your question is too broad and unanswerable at the moment. Please edit it to provide a good minimal reproducible example that shows clearly what you've tried, explain in detail what the code does (including all error messages, if any), what you want instead, and what specifically you are having trouble figuring out. Commented Mar 22, 2017 at 21:04
  • Check this out: stackoverflow.com/questions/30104193/… Commented Mar 27, 2017 at 8:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.