如何在c#中的WPF中将网格中的文本框绑定到二维数组?

时间:2018-12-26 18:20:55

标签: c# wpf

我有由3行3列组成的WPF网格,我需要将它们绑定到此代码中的二维数组,例如Matrix1

class ViewModel
{
    public ViewModel()
    {
        Matrix1 = new double[3, 3];
    }

    double[,] Matrix1 { get; set; }
}

我的xaml代码如下:

<WrapPanel Grid.Row="1" Margin="2 0 0 0" >
    <Border BorderBrush="#00700F" BorderThickness="1" Margin="0 0 10 0">
        <Grid Name="Matrix1" Height="200" Width="200">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBox Grid.Row="0" Grid.Column="0" Width="35" Height="35" VerticalAlignment="Center" Text="{Binding Path=Matrix1[0,0]}"></TextBox>
            <TextBox Grid.Row="0" Grid.Column="1" Width="35" Height="35" VerticalAlignment="Center" Text="{Binding Path=Matrix1[0,1]}"></TextBox>
            <TextBox Grid.Row="0" Grid.Column="2" Width="35" Height="35" VerticalAlignment="Center" Text="{Binding Path=Matrix1[0,2]}"></TextBox>
            <TextBox Grid.Row="1" Grid.Column="0" Width="35" Height="35" VerticalAlignment="Center" Text="{Binding Path=Matrix1[1,0]}"></TextBox>
            <TextBox Grid.Row="1" Grid.Column="1" Width="35" Height="35" VerticalAlignment="Center" Text="{Binding Path=Matrix1[1,1]}"></TextBox>
            <TextBox Grid.Row="1" Grid.Column="2" Width="35" Height="35" VerticalAlignment="Center" Text="{Binding Path=Matrix1[1,2]}"></TextBox>
            <TextBox Grid.Row="2" Grid.Column="0" Width="35" Height="35" VerticalAlignment="Center" Text="{Binding Path=Matrix1[2,0]}"></TextBox>
            <TextBox Grid.Row="2" Grid.Column="1" Width="35" Height="35" VerticalAlignment="Center" Text="{Binding Path=Matrix1[2,1]}"></TextBox>
            <TextBox Grid.Row="2" Grid.Column="2" Width="35" Height="35" VerticalAlignment="Center" Text="{Binding Path=Matrix1[2,2]}" ></TextBox>
        </Grid>
    </Border>
</WrapPanel>

xaml编辑器显示此错误:

  

错误XDG0052未命名的参数“ 0”必须在命名之前出现   论点。 CorConv MainWindow.xaml 34

是否可以通过这种方式绑定文本框?

0 个答案:

没有答案