我已經在此上花了數小時,而類似的主題並沒有幫助。 :(WPF:以編程方式將DataGrid綁定到集合
我有型「圖表」,其中包含一個列表「了LineItem」的對象 我希望以編程方式綁定了LineItem到DataGrid中的用戶控件
用戶控件XAML:。
<DataGrid Name="myData" AutoGenerateColumns="True">
用戶控件代碼背後:
public void SetItemSource(ChartingBase.Chart chart)
{
//DataGrid.ItemsSource = chart.LineItems; // working!
// this is not working:
this.DataContext = chart;
Binding b = new Binding("LineItems");
b.Mode = BindingMode.TwoWay;
b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
myData.SetBinding(DataGrid.ItemsSourceProperty, b);
}
設置只是工作的ItemsSource手動創建綁定不工作,我不知道是什麼即我可以試試。謝謝!
感謝。ObservableCollection - 好吧。 但是:如果Items是我的UserControl中的一個集合,我必須將DataContext設置爲'Self',對吧? 而我不能使用我的對象的副本。 – flowschi
我不確定這是什麼意思,我不知道你的數據來自哪裏......這僅僅是一個例子。無論如何填寫你的收藏。 – Sheridan