我有WPF
應用,其中我使用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");
}
}
}
我相信錯誤「BindingExpression路徑錯誤:在‘對象’未找到‘projectList’屬性」抱怨,因爲'projectList'目前還沒有一個屬性(沒有明確的存取)。 – Chris
您還有'selectedProjectid'的綁定錯誤。我相信那是因爲你忘記了這個綁定上的'ElementName = MainWin'部分 –
@Chris你可以發表你的評論作爲答案。 – Clemens