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 に答える
1
これはおそらく答えではありませんが、コードに根本的な問題があります。
ItemSource
ListBox のプロパティをプロパティにバインドしますEmp
。次に、Click ハンドラーで、型のオブジェクトをListBoxEmp
のプロパティに追加します。Items
これはうまくいきません。
バインディングで機能させるには、EmpList
列挙可能な型、できればObservableCollectionのプロパティが必要です。バインディングは、このプロパティを定義する (モデル) オブジェクトを認識する必要もあります。したがって、ListBox のDataContextを設定するか、バインディングのSourceを指定する必要があります。
データ バインドされた ListBox に要素を追加するときは、要素を に追加するのではItems
なく、バインディングの source プロパティに追加し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);
}
于 2012-08-03T10:01:32.470 に答える