【问题标题】:binding expression error: property not found on object绑定表达式错误:在对象上找不到属性
【发布时间】:2013-06-24 11:21:57
【问题描述】:

我有WPF 应用程序,其中我使用DataBinding 作为comboBox。来自 projectList 的 ProjectName 应该添加到我的 comboBox 中,但是当我运行应用程序时,每次都会收到这些错误;

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“DayView”(名称=“MainWin”)上找不到“projectList”属性。绑定表达式:路径=项目列表; DataItem='DayView'(名称='MainWin');目标元素是 'ComboBox' (Name='');目标属性是“ItemsSource”(类型“IEnumerable”) System.Windows.Data 错误:40:BindingExpression 路径错误:在 'object' ''ComboBox' (Name='')' 上找不到 'selectedProjectid' 属性。 BindingExpression:Path=selectedProjectid;数据项='组合框'(名称='');目标元素是 'ComboBox' (Name='');目标属性是“SelectedValue”(类型“对象”)

我使用数据绑定的 xaml 代码是:

<DataTemplate x:Key="EditableDataTemplate">
            <StackPanel Orientation="Horizontal" Width="596">
                <TextBox Text="{Binding ClientNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
                <TextBox Text="{Binding ApplicationNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
                <TextBox Text="{Binding StartTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
                <TextBox Text="{Binding StopTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
                <TextBox Text="{Binding TaskNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
                <ComboBox x:Name="ComboBox2" ItemsSource="{Binding Path=projectList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedValue="{Binding Path=selectedProjectid}" Width="71" Background="Yellow" BorderThickness="0" DataContext="{Binding RelativeSource={RelativeSource Self}}"/>
            </StackPanel>
        </DataTemplate>

后面的代码是:

public partial class DayView : MetroWindow
{
    private DateTime currentDateForWindow;

    public List<Harvest_Project> projectList;

    public int selectedProjectid{get;set;}

    public DayView(DateTime s)
    {
            InitializeComponent();
             this.DataContext = projectList;
            //this.RootElement.DataContext = myData;
            Globals._globalController.setDayViewWindow(this);

            currentDateForWindow = s;

            dayDateLabel.Content = s.DayOfWeek + ", " + s.Day;
            monthLabel.Content = s.ToString("MMMM");

            listBox1.Items.Clear();

            //projectList = Globals._globalController.harvestManager._PROJECTLIST;
            Globals._globalController.fetchAndPopulateForDate(currentDateForWindow);    
    }

    public void addHarvestEntrytoView(Harvest_TimeSheetEntry entry)
    {
        try
        {
            listBox1.Items.Add(entry);
        }
        catch (Exception)
        { }
    }

    public void addHarvestEntrytoView(List<Harvest_TimeSheetEntry> entry)
    {
        foreach (Harvest_TimeSheetEntry x in entry)
            listBox1.Items.Add(x);
    }

    private void BackButton_Click(object sender, RoutedEventArgs e)
    {
            this.Hide();
            Globals._globalController.getMonthViewWindow.Show();
    }

    private void StartButton_Click(object sender, RoutedEventArgs e)
    {
        Globals._globalController.win32Manager.startTimer();
    }

    private void StopButton_Click_1(object sender, RoutedEventArgs e)
    {
        Globals._globalController.win32Manager.stopTimer();

    }

    private void SyncEntry_Click(object sender, RoutedEventArgs e)
    {
        //Submit All unsynced Entries
    }

    private void ListBoxItem_MouseDoubleClick(object sender, RoutedEventArgs e)
    {
        //Submit clicked Entry
        Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)sender;

        if (!entryToPost.isSynced)
        {
            //Check if something is selected in selectedProjectItem For that item


            if (entryToPost.ProjectNameBinding == "Select Project")
                MessageBox.Show("Please Select a Project for the Entry");
            else
                Globals._globalController.harvestManager.postHarvestEntry(entryToPost);
        }
        else
        {
            //Already synced.. Make a noise or something
            MessageBox.Show("Already Synced;TODO Play a Sound Instead");
        }
    }
}

【问题讨论】:

  • 我相信错误“BindingExpression path error: 'projectList' property not found on 'object'”是在抱怨,因为projectList 当前不是属性(没有定义的访问器)。
  • 您还遇到selectedProjectid 的绑定错误。我相信那是因为您忘记了此绑定上的 ElementName=MainWin 部分
  • @Chris 你可以发表你的评论作为答案。
  • @Chris 谢谢。但是仍然存在 selectedProjectid 的绑定错误。
  • 我也为 ElementName 定义了访问器。因为这也是绑定的一部分。

标签: c# wpf xaml binding-expressions


【解决方案1】:

就像 Chris 提到的,绑定只适用于公共属性。所以你至少要做:

 public List<Harvest_Project> projectList {get;set;}

您的 itemssource {Binding Path=projectList, ElementName=MainWin} 的 xaml 意味着您的元素 MainWin 有一个属性 projectList - 我认为这不是您想要的。

编辑:如果您有任何绑定错误,只需 2 个简单的步骤即可解决此问题

  1. 检查您的 DataContext
  2. 检查您的绑定路径

在运行时,您可以使用 Snoop 执行此任务。

对于您的 selectedProjectid 绑定:您需要一个具有公共属性 selectedProjectid 的 DataContext。如果不是这种情况,您应该检查您的代码

【讨论】:

    【解决方案2】:

    我想补充以上内容。我也遇到了绑定错误。

        System.Windows.Data Error: BindingExpression path error: 'GeneralInformationTopicSectionItemStyles' property not found on 'GeneralInformation.Resources.LocalizationFiles' 'GeneralInformation.Resources.LocalizationFiles' (HashCode=7180698). BindingExpression: Path='GeneralInformationTopicSectionItemStyles.ItemNameTextWithoutSectionFontWeight' DataItem='GeneralInformation.Resources.LocalizationFiles' (HashCode=7180698); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'FontWeight' (type 'System.Windows.FontWeight')
    

    我无法弄清楚问题出在哪里。在我的 LocalizationFiles 类中,我有成员。

        public static object GeneralInformationTopicSectionItemStyles;
    

    看完上面的帖子,我把它从一个成员变成了一个属性。

        public static object GeneralInformationTopicSectionItemStyles
        {
            get;
            set;
        }
    

    还有,瞧!它就像一个魅力。

    MM

    【讨论】:

      【解决方案3】:

      这基本上是blindmeis已经说过的,但我会扩展我的评论=D

      您的错误:"BindingExpression path error: 'projectList' property not found on 'object' " 是您的对象缺少属性定义的结果;在这种情况下,projectList 当前不是属性(没有定义的访问器)。

      您还应该有另一个公共财产来保存您选择的项目(或价值取决于您感兴趣的内容。

      public partial class DayView : MetroWindow
      {
          public List<Harvest_Project> projectList { get; set; }
          public Harvest_Project selectedProject  { get; set; }
      
          // Your other code lives here.
      }
      

      沿这些线绑定(根据需要使用所有元素名称/上下文):

      <ComboBox x:Name="ComboBox2" 
           ItemsSource="{Binding Path=projectList}"
           SelectedItem="{Binding Path=selectedProject}" Mode=TwoWay}" />
      

      如果您希望收到有关 selectedItem 更改的通知,则必须实现 iNotifyPropertyChanged,但这是另一个主题 =D

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-10
        • 1970-01-01
        相关资源
        最近更新 更多