4

I have 3 string arrays like this:

public string[] RowHeaders
{
    get { return new[] {"RowHeader1", "RowHeader2", "RowHeader3", "RowHeader4"}; 
}

public string[] ColumnHeaders
{
    get { return new[] {"ColumnHeader1", "ColumnHeader2", "ColumnHeader3"}; }
}

public string[][] Values
{
    get { return new[]
    {
        new []{"Value11", "Value12", "Value13"},
        new []{"Value21", "Value22", "Value23"},
        new []{"Value31", "Value32", "Value33"},
        new []{"Value41", "Value42", "Value43"},
    }; }
}

Array sizes are unknown until run-time (array values in code snippet are for showing the concept). I want to create a WPF grid from them like

Grid with array data

which binds to these 3 arrays and designed entirely in XAML (if possible). How?

2
  • create newGridControl that inherit from Grid and put there 3 DependencyProperties ColumnHearder, RowHeader, ItemsSource and generate it automatically using code Commented Jan 19, 2015 at 7:58
  • @safi, nice idea. Thank you. I am working on that. Commented Jan 19, 2015 at 8:20

1 Answer 1

6

here a solution using DataTable and MultipleBinding, in the xaml pass the three arrays to an IMultivaluesConverter:

<Grid>
   <DataGrid>
        <DataGrid.ItemsSource>
            <MultiBinding Converter="{StaticResource MatrixToDataViewConverter}">
                <Binding Path="ColumnHeaders"/>
                <Binding Path="RowHeaders"/>
                <Binding Path="Values"/>
            </MultiBinding>
        </DataGrid.ItemsSource>
    </DataGrid>
</Grid>

then in the converter manage those arrays to create a DataView that will be bond to the Grid ItemSource :

 public class MatrixToDataViewConverter:IMultiValueConverter
{            
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var myDataTable = new DataTable();
        var colums = values[0] as string[];
        var rows = values[1] as string[];
        var vals = values[2] as string[][];
        myDataTable.Columns.Add("---");    //The blanc corner column
        foreach (var value in colums)
        {
            myDataTable.Columns.Add(value);
        }
        int index = 0;

        foreach (string row in rows)
        {
            var tmp = new string[1 + vals[index].Count()];                
            vals[index].CopyTo(tmp, 1);
            tmp[0] = row;
            myDataTable.Rows.Add(tmp);
            index++;
        }     
        return myDataTable.DefaultView;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

output : enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, nice solution. Do you have any idea how to make row headers styling like column headers and remove last blank row?
To remove the last blanc row just set the CanUserAddRows : <DataGrid CanUserAddRows="False"> ..and about the Style of the first row Column, i am not sure but try setting the DataGridRowHeader Style
How can I also update the cell background color using the same method?
Quick Question: will the view of the values change if the values 2d array gets changed by some method called during runtime? Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.