簡體   English   中英

Silverlight中的動態Datagrid綁定?

[英]Dynamic Datagrid Binding in Silverlight?

我應該在代碼中動態綁定datagrid。 我寫的代碼如下。 當我調試此代碼塊時,似乎它可以正確進行綁定,但是網格在窗體上沒有列。

MyClass myInstance = new MyClass();
dataGridObject = new DataGrid();
dataGridObject.Width = 200;
dataGridObject.Height = 200;
binding = new Binding();
binding.Source = myInstance;
foreach (PropertyInfo prop in myInstance.GetType().GetProperties())
{
    binding.Path = new PropertyPath(prop.Name);
    DataGridTextColumn column = new DataGridTextColumn();
    column.Header = prop.Name;
    column.Binding = new Binding(prop.Name);
    dataGridObject.Columns.Add(column);
}

dataGridObject.ItemSource = myInstanceList;

盡管我進行了必要的綁定,但為什么不帶列網格? 感謝您的提前答復。

在這種情況下,為什么不將Datagrid的AutoGenerateColumns屬性設置為true? 當此屬性設置為True時,將為您完成編寫的代碼。您要做的就是將對象列表綁定到網格,並且這些列將由網格生成。

您的代碼應該可以正常工作。 您是否已將DataGrid添加到可視樹。 例如

LayoutRoot.Children.Add(dataGridObject)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM