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
  • 24,892
  • 5
  • 60
  • 84
nemo
  • 23
  • 2

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
  • 71,009
  • 55
  • 247
  • 290