wpf数据网格填充数据
本文关键字:数据 填充 网格 数据网 wpf | 更新日期: 2023-09-27 18:27:53
使用动态源填充数据网格存在一些问题。例如,我想用具有各种参数的对象填充数据网格。此参数显示数据库中的动态数据。列计数可以改变,参数计数可以改变但计数相等。填充标头:
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]),但行只填充具有属性的对象。我可以用动态长度填充数据网格吗?
没有解决方案,而是创建从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);
}