2013-06-24 305 views
3

我有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"); 
     } 
    } 
} 
+3

我相信錯誤「BindingExpression路徑錯誤:在‘對象’未找到‘projectList’屬性」抱怨,因爲'projectList'目前還沒有一個屬性(沒有明確的存取)。 – Chris

+0

您還有'selectedProjectid'的綁定錯誤。我相信那是因爲你忘記了這個綁定上的'ElementName = MainWin'部分 –

+0

@Chris你可以發表你的評論作爲答案。 – Clemens

回答

6

像克里斯提到,結合剛剛與公衆poperties工作。所以你必須至少要做到:

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

你的XAML的ItemsSource {綁定路徑= projectList,的ElementName =的MainWin}意味着你的元素MainWin能夠有一個屬性projectList - 我認爲這是你想要的不是什麼。

編輯:如果您有任何約束力的錯誤,也有隻是兩個簡單的步驟來解決這個

  1. 檢查您的DataContext
  2. 在運行時檢查你的綁定路徑

您可以使用Snoop爲這個任務。

對於您選擇的項目綁定:您期望一個具有公共屬性selectedProjectid的DataContext。如果它不是你案件應檢查你的代碼

2

這基本上是什麼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}" /> 

你必須執行INotifyPropertyChanged如果您希望被通知的selectedItem屬性的變化,但這是另一個話題= d

4

我想添加到上面。我也有一個綁定錯誤。

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

+0

這個,謝謝! –

相關問題