gpt4 book ai didi

c# - 绑定(bind)表达式错误 : property not found on object

转载 作者:行者123 更新时间:2023-11-30 13:26:48 24 4
gpt4 key购买 nike

我有一个 WPF 应用程序,我在其中为 comboBox 使用了 DataBinding。 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')

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

最佳答案

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

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

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

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

  1. 检查您的 DataContext
  2. 检查你的绑定(bind)路径

在运行时你可以使用Snoop为了这个任务。

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

关于c# - 绑定(bind)表达式错误 : property not found on object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17274255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com