使用动态源填充数据网格存在一些问题。例如,我想用具有各种参数的对象填充datagrid。此参数显示数据库中的动态数据。列数可能会更改,参数计数可能会更改但计数等于。填充标题:
private void DataSourseChanged(SourceList sourceList)
{
Columns.Clear();
Columns.Add(new DataGridTextColumn());
if (sourceList != null)
{
foreach (var item in sourceList.ColumnsHeaders)
Columns.Add(new DataGridTextColumn { Header = item });
}
}
public class SourceList
{
private readonly IList _columnsHeaders;
private readonly IList _rowsHeaders;
private readonly IList _dataRows;
public IList ColumnsHeaders
{
get { return _columnsHeaders; }
}
public IList RowsHeaders
{
get { return _rowsHeaders; }
}
public IList DataRows
{
get { return _dataRows; }
}
public SourceList(IList rowsHeaders, IList columnsHeaders, IList dataRows)
{
_rowsHeaders = rowsHeaders;
_columnsHeaders = columnsHeaders;
_dataRows = dataRows;
}
}
我想填充标题和行(第一列中的_rowsHeaders [i]和_datarows [i]的其他列合并的行),但行只填充具有属性的对象。我可以用动态长度填充数据网格吗?
答案 0 :(得分:0)
没有解决方案,但创建从System.Windows.Controls.Grid派生的自定义网格,并为填充网格创建自定义逻辑
public static readonly DependencyProperty CustomSourceProperty
= DependencyProperty.Register(
"CustomSource",
typeof(SourceList),
typeof(InteractiveGrid),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
CustomSourceChanged
)
);
[Category("Group properties")]
public SourceList CustomSource
{
get { return (SourceList)GetValue(CustomSourceProperty); }
set { SetValue(CustomSourceProperty, value); }
}
private void CustomSourceChanged(SourceList sourceList)
{
//...
}
private static void CustomSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((InteractiveGrid)d).CustomSourceChanged((SourceList)e.NewValue);
}