1

Due to Two way binding between DataGrid and an array I want to make such a grid, but there is still an error and I don't know how to eliminate it.

Here is all the code:

public partial class MainWindow : Window
{
    private string[,] _data2D;
    public string[,] Data2D
    {
        get { return _data2D; }
        set { _data2D = value; }
    }
    public MainWindow()
    {
        _data2D = new string[9 , 9];
        for (int i = 0; i < 9; i++)
            for (int j = 0; j < 9; j++)
                _data2D[i, j] = "0";
        InitializeComponent();
        dataGrid2D.DataContext = this;
    }
}

And XAML:

<Window x:Class="test.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:test"
        mc:Ignorable="d"
        xmlns:dg2d="clr-namespace:DataGrid2DLibrary;assembly=DataGrid2DLibrary"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <dg2d:DataGrid2D Background="Azure" UseModifiedDataGridStyle="true" HorizontalAlignment="Left" VerticalAlignment="Top" ColumnWidth="30" DataContext="this"
                         RowHeight="30" HeadersVisibility="None" Name="dataGrid2D" ItemsSource2D="{Binding Data2D}"/>
    </Grid>
</Window>

and the error:

System.Windows.Data Error: 40 : 

BindingExpression path error: 

'Data2D' property not found on 'object' ''String' (HashCode=1178749465)'. 

BindingExpression:Path=Data2D; DataItem='String' (HashCode=1178749465);

target element is 'DataGrid2D' (Name='dataGrid2D'); 

target property is 'ItemsSource2D' (type 'IEnumerable')

Anyone knows how to help me? It makes sometimes not all controls rendering :/ Sometimes some control render in half etc.

Community
  • 1
  • 1
Blabla
  • 367
  • 4
  • 16

1 Answers1

1

You set DataContext in two places:

In MainWindow():

dataGrid2D.DataContext = this;

And in XAML you have:

DataContext="this"

Delete DataContext property from your XAML:

<dg2d:DataGrid2D Background="Azure" UseModifiedDataGridStyle="true" HorizontalAlignment="Left" VerticalAlignment="Top" ColumnWidth="30" 
                         RowHeight="30" HeadersVisibility="None" Name="dataGrid2D" ItemsSource2D="{Binding Data2D}"/>
Roman
  • 11,966
  • 10
  • 38
  • 47