不工作绑定

时间:2015-05-27 14:36:03

标签: c# wpf xaml mvvm binding

事先抱歉我的英语:)

XAML标记:

ListView

C#代码:

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

为什么绑定在除此行之外的任何地方都有效:

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

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

1 个答案:

答案 0 :(得分:0)

结帐ReSharper WPF error: "Cannot resolve symbol "MyVariable" due to unknown DataContext"。在答案的最后,它显示了如何使用免费的Snoop实用程序在运行时识别任何错误的DataContext。