[英]wpf datagrid Ienumerable returning null
使用C#.NET4.5,MS Visual Studio 2012,WPF。
大家好,這里有一些代碼給了我null。 這段代碼是我嘗試過的以前的解決方案。 從今天開始,我發現那里的行為空,所以我沒有收到任何錯誤,但從未進行過測試。
這是代碼:
1首先,我將從SQL收集的數據放入數據表中,然后放入數據網格中...
private void LoadPareto(string pg)
{
DataTable tbl = new DataTable();
tbl = mysqlq.SQL_GetPareto(pg);
paretogrid.ItemsSource = tbl.AsDataView();
// InsertColumns();
ShowArrows();
}
第二個在XAML中設置綁定...
<DataGrid Name ="paretogrid" ItemsSource="{Binding}"
第三,我創建了一個無數的...
public IEnumerable<System.Windows.Controls.DataGridRow> GetDataGridRow(System.Windows.Controls.DataGrid grid)
{
var itemsource = grid.ItemsSource as System.Collections.IEnumerable;
if (null == itemsource) yield return null;
foreach (var item in itemsource)
{
var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow; // null?
if (null != row) yield return row;
}
}
然后我用這種方法來稱呼它...
private void ShowArrows()
{
var rows = GetDataGridRow(paretogrid); // fetching null?
foreach (DataGridRow r in rows)
{
DataRowView rv = (DataRowView)r.Item;
foreach (DataGridColumn column in paretogrid.Columns)
{
if (column.GetCellContent(r) is TextBlock)
{
TextBlock cellcontent = column.GetCellContent(r) as TextBlock;
MessageBox.Show(cellcontent.Text);
}
}
}
}
現在我遇到的問題在Ienumerbale中,我看到我的物料來源包含12007條記錄,這是完美的。 然而,當我逐步通過時,我發現...
var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
返回空值,我的“ if”語句將其查找為false,因此跳過了收益。 因此,當然,當我進入“ showarrows”方法中的foreach循環時,因為沒有null,所以它不會打擾。
那我哪里錯了? 我錯過了什么嗎?
在此先感謝你們n女孩!
獲取行時,您的DataGrid
尚未更新。 解決方案是在設置ItemsSource
之后立即使用paretogrid.UpdateLayout()
。
請注意,將數據和UI代碼混合在一起並不是很好。 您嘗試執行的操作可能已經在XAML中完成,並且不會出現此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.