Natxo
Natxo

Reputation: 2947

GridViewRowPresenter using Datagrid's columns

Is there any way of feeding the columns of a Datagrid to a GridViewRowPresenter ?

It cannot be done directly, as one uses DataGridColumn and the other GridViewColumn so this doesn't work:

<GridViewRowPresenter Columns="{Binding ElementName=myDataGrid, Path=Columns, Mode=OneWay}" />

Upvotes: 1

Views: 173

Answers (1)

adminSoftDK
adminSoftDK

Reputation: 2092

I haven't tried this, but something like this should work:

public class MyGridViewRowPresenter : GridViewRowPresenter
{
    public static readonly DependencyProperty NumberOfColumnsProperty = DependencyProperty.Register("NumberOfColumns", typeof(int), typeof(MyGridViewRowPresenter), new PropertyMetadata(0));

    public int NumberOfColumns
    {
        get { return (int)GetValue(NumberOfColumnsProperty); }
        set { SetValue(NumberOfColumnsProperty, value); }
    }

    public override void EndInit()
    {
        base.EndInit();
        for (var i = 0; i < NumberOfColumns; i++)
        {
            Columns.Add(new GridViewColumn());
        }
    }
}

usage

 <local:MyGridViewRowPresenter NumberOfColumns="{Binding ElementName=myDataGrid, Path=Columns.Count, Mode=OneWay}" />

I did something similar to a standard grid, so instead of doing row or column definitions and then add column and rows, i would just say columns=somenumber and that would do it.

Upvotes: 1

Related Questions