【问题标题】:wpf datagrid filling datawpf datagrid 填充数据
【发布时间】:2014-10-27 08:40:01
【问题描述】:

使用动态源填充数据网格存在一些问题。例如,我想用具有各种参数的对象填充数据网格。此参数显示来自数据库的动态数据。列数可能会更改,参数计数可能会更改但计数相等。填写标题:

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] 的合并),但行仅填充具有属性的对象。我可以用动态长度填充数据网格吗?

【问题讨论】:

标签: c# wpf datagrid


【解决方案1】:

没有解决方案,但创建从 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 2011-09-24
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    相关资源
    最近更新 更多