数据绑定错误40 dataGrid2d

时间:2017-03-06 15:16:08

标签: c# wpf data-binding

由于Two way binding between DataGrid and an array我想制作这样的grid,但仍然存在错误,我不知道如何消除它。

以下是所有代码:

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;
    }
}

和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>

和错误:

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')

任何人都知道如何帮助我?它有时并不是所有的控件渲染:/有时一些控件渲染一半等。

1 个答案:

答案 0 :(得分:1)

您在两个地方设置了DataContext

MainWindow()

dataGrid2D.DataContext = this;

XAML你有:

DataContext="this"

DataContext

中删除XAML媒体资源
<dg2d:DataGrid2D Background="Azure" UseModifiedDataGridStyle="true" HorizontalAlignment="Left" VerticalAlignment="Top" ColumnWidth="30" 
                         RowHeight="30" HeadersVisibility="None" Name="dataGrid2D" ItemsSource2D="{Binding Data2D}"/>