2012-08-03 70 views
0
public class Emp 
    { 
     public string Id { get; set; } 

    } 

I declared the class like this, but property i didnt set. I set the dependency property for textblock 

public static readonly DependencyProperty LabelProperty 
    = DependencyProperty.Register("TextBlock", typeof(string), typeof(WindowsPhoneControl1), new PropertyMetadata(new PropertyChangedCallback(LabelChanged))); 

public string List 
    { 
     get { return (string)GetValue(LabelProperty); } 
     set { SetValue(LabelProperty, value); } 
    } 

回答

1

這可能不是答案,但是你的代碼有一些根本性的錯誤。

您將列表框的ItemSource屬性綁定到屬性Emp。然後在Click處理程序中,將Emp類型的對象添加到ListBox的Items屬性中。這不起作用。

爲了使它與綁定一起工作,必須有一些枚舉類型的屬性EmpList,最好是ObservableCollection。該綁定還需要知道定義此屬性的(模型)對象。因此,您必須設置列表框的DataContext或您指定綁定的Source

將元素添加到數據綁定列表框時,不會將它們添加到Items,而是將它們添加到綁定的源屬性EmpList

public class Model 
{ 
    private ICollection<Emp> empList = new ObservableCollection<Emp>(); 

    public ICollection<Emp> EmpList { get { return empList; }} 
} 

綁定是這樣的:

<ListBox ItemsSource="{Binding EmpList, Source={ an instance of Model }}" ... /> 

或者像下面

<ListBox Name="listBox" ItemsSource="{Binding EmpList}" ... /> 

,並設置DataContext的,也許在代碼:

listBox.DataContext = model; // where model is an instance of class Model 

for (int i = 0; i < result.Length; i++)   
{   
    Emp data = new Emp { Id = result[i] }; 
    model.EmpList.Add(data); 
} 
+0

沒有還我得到了相同的結果。沒有項目在列表中展示。綁定路徑是否正確,但是'Id'必須是公共屬性。我必須給datatemplate內的文本塊綁定什麼路徑 – 2012-08-03 11:26:06

+0

綁定路徑可以,但是'Id'必須是公共屬性。您是否在Visual Studio輸出窗口中看到任何綁定錯誤消息? – Clemens 2012-08-03 11:37:12

+0

輸出窗口中顯示沒有錯誤信息。 – 2012-08-06 05:41:39