0

I'm tryng to bind a 2 dimensional array to a DataGrid in WPF. I found this library DataGrid2D but I make some mistake because it does not display the correct result but an empty grid.

This is my XAML code:

 .....
  xmlns:dataGrid2D="http://gu.se/DataGrid2D"
 ....

 <Grid>
     <DataGrid Name="matrix" dataGrid2D:ItemsSource.Array2D="{Binding Data2D}"
                              dataGrid2D:ItemsSource.ColumnHeadersSource="{Binding ColumnHeaders}"
                              dataGrid2D:ItemsSource.RowHeadersSource="{Binding RowHeaders}" />
 </Grid>

And this is the .cs

private String[] _columnHeaders;
public String[] ColumnHeaders
{
    get { return _columnHeaders; }
    set { _columnHeaders = value; }
}

private String[] _rowHeaders;
public String[] RowHeaders
{
    get { return _rowHeaders; }
    set { _rowHeaders = value; }
}

private bool[,] _data2D;
public bool[,] Data2D
{
    get { return _data2D; }
    set { _data2D = value; }
}


String[] columnHeaders = { "A", "B", "C" };
String[] rowHeaders = { "1", "2", "3" };
bool[,] data2D = { { true, true, false }, { true, true, false }, { true, true, false } };


ColumnHeaders = columnHeaders;
RowHeaders = rowHeaders;
Data2D = data2D;
CZoellner
  • 13,553
  • 3
  • 25
  • 38
ab_mundi
  • 53
  • 10

1 Answers1

-1

I was able to get this to work by moving the property setters into my viewmodel constructor and ensuring the viewmodel is set as the datacontext in the xaml.

ViewModel:

public class MainWindowViewModel
{
    private String[] _columnHeaders;
    public String[] ColumnHeaders
    {
        get { return _columnHeaders; }
        set { _columnHeaders = value; }
    }

    private String[] _rowHeaders;
    public String[] RowHeaders
    {
        get { return _rowHeaders; }
        set { _rowHeaders = value; }
    }

    private bool[,] _data2D;
    public bool[,] Data2D
    {
        get { return _data2D; }
        set { _data2D = value; }
    }


    String[] columnHeaders = { "A", "B", "C" };
    String[] rowHeaders = { "1", "2", "3" };
    bool[,] data2D = { { true, true, false }, { true, true, false }, { true, true, false } };

    public MainWindowViewModel()
    {
        ColumnHeaders = columnHeaders;
        RowHeaders = rowHeaders;
        Data2D = data2D;
    }
}

Xaml:

<Window x:Class="Application.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Application"
    xmlns:dataGrid2D="http://gu.se/DataGrid2D"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
  <Window.DataContext>
    <local:MainWindowViewModel />
  </Window.DataContext>
  <Grid>
    <DataGrid Name="matrix" 
              dataGrid2D:ItemsSource.Array2D="{Binding Data2D}"
              dataGrid2D:ItemsSource.ColumnHeadersSource="{Binding ColumnHeaders}"
              dataGrid2D:ItemsSource.RowHeadersSource="{Binding RowHeaders}" />
  </Grid>
</Window>
Kyle Hancock
  • 132
  • 8