0

I Have a WPF treeview and i need the reference of parent node in the child node context. menu command. In the below XAML, i need to pass the reference of A in member command parameter

XAML:

<DataTemplate x:Key="Member">
 <TextBlock Text="{Binding}" Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=mylib:ExtendedTreeView}}">
  <TextBlock.ContextMenu>
   <ContextMenu>
    <MenuItem Header="Delete" Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.Tag.DeleteMmeberCommand}">
     <MenuItem.CommandParameter>
      <MultiBinding Converter="{StaticResource MutilValueConverter}">
       <Binding Path=".."/>
        <Binding />
      </MultiBinding>
     </MenuItem.CommandParameter>
    </MenuItem>
   </ContextMenu>
  </TextBlock.ContextMenu>
 </TextBlock>
</DataTemplate>
<HierarchicalDataTemplate DataType="{x:Type A}" ItemsSource="{Binding Members}" ItemTemplate="{StaticResource Member}"
 <TextBlock Text="{Binding"}>
  <TextBlock.ContextMenu>
   <ContextMenu>
    <MenuItem Header="Delete" Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.Tag.DeleteACommand}" CommandParameter="{Binding}"/>
   </ContextMenu>
  </TextBlock.ContextMenu>
 </TextBlock>
</HierarchicalDataTemplate>


<TreeView ItemsSource="{Binding As}"/>

Converter:

public class MutilValueConverter : IMultiValueConverter
{
    public object  Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values;
    }

    public object[]  ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
mdb
  • 52,000
  • 11
  • 64
  • 62
Arihant
  • 367
  • 1
  • 6
  • 22

3 Answers3

0

If i understand correctly, you could possibly invert the whole thing:

publish your command in what is your datacontext and give an instance of your subdatacontext as command parameter (this is just Binding for your items)

Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
  • The command is already published in the data context.. Basically the command paremeter should have 2 items, one the parent item which is A, and the child item being deleted that is a member – Arihant Jun 07 '12 at 08:17
  • if you have child viewmodels, let the command be published by the parent view model and the parameter is the child you want to delete. the parent is then just "this" and the child is the parameter. – Mare Infinitus Jun 07 '12 at 08:23
  • Child is not a view model. It is a string. Parent is model object that has collection of string. So on right click of that string, i need to remove that from Parent – Arihant Jun 07 '12 at 08:26
  • if you treat that string as a viewmodel, things get easier. just write a small class implementing inotifypropertychanged with a public string property and a command (the delete command) and make members an observablecollection of what is your child viewmodel. – Mare Infinitus Jun 07 '12 at 08:30
  • Have objects as VM would make the view heavy. – Arihant Jun 07 '12 at 08:32
  • i don't believe that this is that heavy as it is more flexible in return. – Mare Infinitus Jun 07 '12 at 08:35
  • Please let us know what solution you applied and whether the hints here were helpful. – Mare Infinitus Jun 07 '12 at 11:43
  • Still waiting for the solution – Arihant Jun 07 '12 at 15:32
0

You are using PlacementTaregt in your bindings but you havent set the ContextMenu.PlacementTarget anywhere...

  <TextBlock Text="{Binding"} x:Name="MyTextBox">
    <TextBlock.ContextMenu>
      <ContextMenu PlacementTarget="{Binding ElementName=MyTextBox}"> 
        .....
WPF-it
  • 19,625
  • 8
  • 55
  • 71
0

the straight way is to have viewmodels for what your Members collection is holding.

and with child viewmodels, there is no need for getting in the binding, as you can just hold the data needed in the viewmodel class. it is an adapter between your model (whereever the strings come from) and your view (where the strings are displayed).

Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113