【问题标题】:Not working binding不工作绑定
【发布时间】:2015-05-27 14:36:02
【问题描述】:

提前对不起我的英语:)

XAML 标记:

<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#代码:

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

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

为什么除了这一行之外,绑定在任何地方都有效:

 <MenuItem Header="Add" Command="{Binding CommandAddDataRow}"/>

【问题讨论】:

  • CommandAddDataRow = new Command(AddDataRow);命令是什么类?
  • 公共类 Command : ICommand
  • 我不确定 Command&lt;T&gt; 类(你自己的?),但你认为 dataRow 参数的参数来自哪里?查看您的输出窗口,那里可能存在一些绑定错误。
  • 这里有问题。 Command的构造函数定义是什么?

标签: c# wpf xaml mvvm binding


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 2016-08-05
    • 2012-07-31
    • 2014-02-19
    相关资源
    最近更新 更多