2013-06-20 83 views
0

我搜索了並找不到答案我的問題有人可以幫忙嗎?我試圖將一個對象列表綁定到一個列表框。我有正確數量的項目(我仍然可以選擇它們),但我看不到文本。WPF綁定對象到列表框,文本不出現

我的XAML代碼:

<ListBox x:Name="listbox_leave" BorderThickness="0"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="{Binding leaveName}" /> 
       <TextBlock Text="{Binding numberOfDays}" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

這是後端代碼:

ObservableCollection<Leave> leavelist = new ObservableCollection<Leave>(); 
leavelist = DbControl.loadLeaveDetails(); 
listbox_leave.ItemsSource = leavelist; 

和我的假類

class Leave 
{ 
    public string leaveName; 

    public string LeaveName 
    { 
     get { return leaveName; } 
     set { leaveName = value; } 
    } 

    public string numberOfDays; 

    public string NumberOfDays 
    { 
     get { return numberOfDays; } 
     set { numberOfDays = value; } 
    } 
} 

當我調試,休假ObservableCollection列表中有所有正確的數據,我的列表框顯示空白,但它有正確數量的對象留在它(我可以選擇他們),但沒有文字顯示。我有這個味精:

System.Windows.Data Error: 40 : BindingExpression path error: 'leaveName' property not found on 'object' ''Leave' (HashCode=44930696)'. BindingExpression:Path=leaveName; DataItem='Leave' (HashCode=44930696); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 

System.Windows.Data Error: 40 : BindingExpression path error: 'numberOfDays' property not found on 'object' ''Leave' (HashCode=44930696)'. BindingExpression:Path=numberOfDays; DataItem='Leave' (HashCode=44930696); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 

System.Windows.Data Error: 40 : BindingExpression path error: 'leaveName' property not found on 'object' ''Leave' (HashCode=29274103)'. BindingExpression:Path=leaveName; DataItem='Leave' (HashCode=29274103); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 

System.Windows.Data Error: 40 : BindingExpression path error: 'numberOfDays' property not found on 'object' ''Leave' (HashCode=29274103)'. BindingExpression:Path=numberOfDays; DataItem='Leave' (HashCode=29274103); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 

有人可以幫忙?

回答

0

您試圖綁定到字段(leaveNamenumberOfDays)而不是屬性(LeaveNameNumberOfDays),這是不是在WPF支持。

將其更改爲:

<ListBox x:Name="listbox_leave" BorderThickness="0"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
      <TextBlock Text="{Binding LeaveName}" /> 
      <TextBlock Text="{Binding NumberOfDays}" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

此外,你應該繼續前進,使代替public領域privateleaveNamenumberOfDays)。