c# – 绑定表达式错误:在对象上找不到属性

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

System.Windows.Data Error: 40 : BindingExpression path error: ‘projectList’ property not found on ‘object’ ”DayView’ (Name=’MainWin’)’. BindingExpression:Path=projectList; DataItem=’DayView’ (Name=’MainWin’); target element is ‘ComboBox’ (Name=”); target property is ‘ItemsSource’ (type ‘IEnumerable’)
System.Windows.Data Error: 40 : BindingExpression path error: ‘selectedProjectid’ property not found on ‘object’ ”ComboBox’ (Name=”)’. BindingExpression:Path=selectedProjectid; DataItem=’ComboBox’ (Name=”); target element is ‘ComboBox’ (Name=”); target property is ‘SelectedValue’ (type ‘Object’)

我使用数据绑定的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");
        }
    }
}

解决方法

像克里斯提到的那样,只与公共场合合作.所以你必须至少做到:

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

你的xaml for itemssource {Binding Path = projectList,ElementName = MainWin}意味着你的元素MainWin有一个Property projectList – 我认为那不是你想要的.

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

>检查您的DataContext
>检查您的绑定路径

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

对于您的selectedProjectid绑定:您期望具有selectedProjectid公共属性的DataContext.如果不是这样,你应该检查你的代码

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...