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

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

概述我有 WPF applcation,其中我使用了DataBinding作为comboBox. projectList中的ProjectName应该在我的comboBox中添加,但是当我运行应用程序时,每次我都会收到这些错误; System.Windows.Data Error: 40 : BindingExpression path error: ‘projectList’ property no 我有 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.如果不是这样,你应该检查你的代码

总结

以上是内存溢出为你收集整理的c# – 绑定表达式错误:在对象上找不到属性全部内容,希望文章能够帮你解决c# – 绑定表达式错误:在对象上找不到属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1222485.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存