WPF datagrid行标题绑定 - Observable集合

时间:2016-11-28 19:26:47

标签: c# wpf xaml datagrid observablecollection

我使用datatable构建了一个数据网格,结果显示正确。 现在我打算将ROWHEADER添加到datagrid。我填充了observable collection<string>,在XAML中,我将其用作行标题数据模板路径。它仍然没有出现。有人可以建议问题在哪里吗?

public ObservableCollection<string> Rownameheaders { get; set; } //in the main .cs

//xaml.cs
public void Form1_load(object sender, EventArgs e)
{
    WF_CRM_RPluginModel model = DataContext as WF_CRM_RPluginModel;
    var hello1 =  new ObservableCollection<String>();

    for(int i=0; i< model.prodwells.Count;i++)
    {
        hello1.Add(model.prodwells[i]);
    }

    model.Rownameheaders = hello1;

    List<double[]> list = new List<double[]>();
    for (int i = 0; i < model.RowFijtable; i++)
    {
        double[] rowdata = new double[model.ColFijtable];
        for (int j = 0; j < model.ColFijtable; j++)
        {
            rowdata[j] = model.TauTable[i, j];
        }

        list.Add(rowdata);
    }

    DataTable table = ConvertListToDataTable(list);

    dataGridView1.ItemsSource = table.AsDataView();  
}

private DataTable ConvertListToDataTable(List<double[]> list)
{
    DataTable table = new DataTable();
    WF_CRM_RPluginModel model = DataContext as WF_CRM_RPluginModel;
    // Get max columns.
    int columns = 0;
    foreach (var array in list)
    {
        if (array.Length > columns)
        {
            columns = array.Length;
        }
    }

    // Add columns.
    for (int i = 0; i < columns; i++)
    {
        // Provide default column name & data type
        table.Columns.Add(model.injwells[i], typeof(double));
    }

    // Add rows.
    foreach (var array in list)
    {
        // assign each array element to the appropriate column
        var row = table.NewRow();

        for (int i = 0; i < array.Length; ++i)
            row.SetField(i, array[i]);

        table.Rows.Add(row);
    }

    return table;
}

XAML:

<DataGrid Name="dataGridView1" Loaded="Form1_load" ItemsSource="{Binding}" AutoGenerateColumns="True"                                          AlternatingRowBackground="Gainsboro" CanUserSortColumns="False">
    <DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}, Path=Rownameheaders}"/>
        </DataTemplate>
    </DataGrid.RowHeaderTemplate> 
</DataGrid>

这是我得到的结果但没有行标题......

enter image description here

1 个答案:

答案 0 :(得分:0)

我最后添加了一个包含值的新列,并在数据表中将列位置设置为0。以下是更新后的代码:

private DataTable ConvertListToDataTable(List<double[]> list)
{
    DataTable table = new DataTable();
    WF_CRM_RPluginModel model = DataContext as WF_CRM_RPluginModel;
    // Get max columns.
    int columns = 0;
    foreach (var array in list)
    {
        if (array.Length > columns)
        {
            columns = array.Length;
        }
    }

   // Add columns.
    for (int i = 0; i < columns; i++)
    {
        // Provide default column name & data type
        table.Columns.Add(model.injwells[i], typeof(double));

    }

    // Add rows.
    foreach (var array in list)
    {
        // assign each array element to the appropriate column
        var row = table.NewRow();
        for (int i = 0; i < array.Length; ++i)
            row.SetField(i, array[i]);
        table.Rows.Add(row);
    }

    //ADDED below part
    DataColumn newCol = new DataColumn("Prod Name", typeof(string));
    newCol.AllowDBNull = true;
    table.Columns.Add(newCol);
    newCol.SetOrdinal(0);
    int m = 0;
    foreach(DataRow row in table.Rows)
    {
        row["Prod Name"] = model.prodwells[m];
        m = m + 1;
    }

    return table;
}

希望它也能帮助别人!