2

In advance sorry for my english :)

XAML markup:

<TreeView x:Name="treeRows" ItemsSource="{Binding TreeRows}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <Setter Property="IsExpanded" Value="True"></Setter>
        </Style>                                    
     </TreeView.ItemContainerStyle>
     <TreeView.ItemTemplate>
         <HierarchicalDataTemplate ItemsSource="{Binding Children}">
             <Label Content="{Binding DisplayColumn}"></Label>
             <HierarchicalDataTemplate.ItemContainerStyle>
                 <Style TargetType="TreeViewItem">
                     <Style.Triggers>
                         <EventTrigger RoutedEvent="MouseRightButtonDown">
                             <EventTrigger.Actions>
                                 <BeginStoryboard>
                                     <Storyboard>
                                         <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
                                             <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
                                         </BooleanAnimationUsingKeyFrames>
                                     </Storyboard>
                                 </BeginStoryboard>
                             </EventTrigger.Actions>
                         </EventTrigger>
                     </Style.Triggers>
                     <Setter Property="ContextMenu">
                         <Setter.Value>
                             <ContextMenu>
                                 <MenuItem Header="Add" Command="{Binding CommandAddDataRow}"/>
                                 <MenuItem Header="Update"/>
                                 <MenuItem Header="Delete"/>
                             </ContextMenu>
                         </Setter.Value>
                     </Setter>
                     <Setter Property="IsExpanded" Value="True"></Setter>
                 </Style>
             </HierarchicalDataTemplate.ItemContainerStyle>
         </HierarchicalDataTemplate>
     </TreeView.ItemTemplate>
 </TreeView>

C# code:

public class MainWindow 
{
    public ICommand CommandAddDataRow { get; set; }
    public void AddDataRow(DataRow dataRow)
    {

    }
    public MainWindow()
    {
        CommandAddDataRow = new Command<DataRow>(AddDataRow);
    }
}

Why does the binding work everywhere except for this line:

 <MenuItem Header="Add" Command="{Binding CommandAddDataRow}"/>
Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
nemo
  • 23
  • 2
  • CommandAddDataRow = new Command(AddDataRow); What Class is Command? – Galma88 May 27 '15 at 15:50
  • public class Command : ICommand – nemo May 27 '15 at 16:27
  • 1
    I'm not sure about that `Command` class (your own?) but where do you think the argument for the dataRow parameter is coming form? Look in your Output window, there probably is some binding error there. – H H May 28 '15 at 06:45
  • There's something wrong here. What is the definition of the constructor of Command? – Galma88 May 28 '15 at 07:22

1 Answers1

0

Check out ReSharper WPF error: "Cannot resolve symbol "MyVariable" due to unknown DataContext". At the end of the answer, it shows how to use the free Snoop utility to identify any bad DataContext at runtime.

Community
  • 1
  • 1
Contango
  • 76,540
  • 58
  • 260
  • 305