C# 如何将字符串数组自动绑定到WPF数据网格?

C# 如何将字符串数组自动绑定到WPF数据网格?,c#,arrays,wpf,data-binding,datagrid,C#,Arrays,Wpf,Data Binding,Datagrid,我有一个DataGrid在UserControl中。看起来是这样的: <UserControl x:Class="ExternalDataSourceComparison.ImportedInfoGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

我有一个
DataGrid
UserControl
中。看起来是这样的:

<UserControl x:Class="ExternalDataSourceComparison.ImportedInfoGrid"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         Height="Auto" Width="Auto">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <DataGrid Name="_dataGrid" Grid.Row="0">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path[0].Length}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
<Window x:Class="ExternalDataSourceComparison.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ExternalDataSourceComparison"
    Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <local:ImportedInfoGrid x:Name="ExternalData" Grid.Row="0"/>
    <StackPanel Height="36px" Orientation="Horizontal" Grid.Row="1">
        <Button Name="_import" Content="Import Data" Margin="5" Padding="5, 2" Click="Import_Click"/>
        <Button Name="_compare" Content="Compare" Margin="5" Padding="5, 2" Click="Compare_Click"/>
        <Button Name="_cancel" Content="Cancel" Margin="5" Padding="5, 2" Click="Cancel_Click"/>            
    </StackPanel>
</Grid>

在使用方法
fs.CSVToStringArray
的窗口代码隐藏中,我打开一个CSV文件,将内容解析为
string[][]
外部数组表示行,内部数组表示所有列,因此string[0][3]将是第1行第4列

在我的代码隐藏中,我只是将
ItemsSource
设置为数组的数组,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace ExternalDataSourceComparison
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        FileStuff fs = new FileStuff();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Import_Click(object sender, RoutedEventArgs e)
        {
            string[][] array = fs.CSVToStringArray();
            this.ExternalData._dataGrid.ItemsSource = array;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Compare_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用系统数据;
命名空间外部数据源比较
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
FileStuff fs=newfilestuff();
公共主窗口()
{
初始化组件();
}
私有无效导入\单击(对象发送方,路由目标)
{
字符串[][]数组=fs.CSVToStringArray();
this.ExternalData.\u dataGrid.ItemsSource=array;
}
私有无效取消\单击(对象发送者,路由目标)
{
}
私有无效比较\u单击(对象发送者,路由目标e)
{
}
}
}

但输出结果并不是我所期望的。我认为它显示的是对象的属性,而不是实际内容。有没有一种方法可以自动生成行和列,或者我必须编写代码来创建列和生成行?我认为使用
DataGrid
可以将
string[][]
List
之类的东西绑定到
ItemsSource
上,它只会生成列。它目前正在自动生成列,只是没有用
字符串[][]

DataGrid
的自动生成列功能不太适合显示锯齿状数组数据。它更适合于单个一维集合,
DataGrid
中的每一行表示集合中的单个项,每一列表示对应项的不同属性

除非
ItemsSource
中的项目是准备好显示的模型,否则不应依赖自动生成列功能(它将显示所有属性的列,包括那些不打算显示的属性)。尝试将
DataGrid
自动绑定到数组时的另一个问题是,
DataGrid
没有关于如何命名列的信息

最后,我认为您最好以某种方式定义
DataGrid
列,而不是让
DataGrid
自己生成列。或者从XAML定义它,例如,假设数组中始终有两个“列”:

<DataGrid Name="_dataGrid" Grid.Row="0" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Column 1" Binding="{Binding [0]}"/>
        <DataGridTextColumn Header="Column 2" Binding="{Binding [1]}"/>
    </DataGrid.Columns>
</DataGrid>

或者从代码中定义列,如果列数是动态的,例如:

string[][] array = fs.CSVToStringArray();
for (int i = 0; i < array[0].Length; i++)
{
    var col = new DataGridTextColumn();
    col.Header = "Column " + i;
    col.Binding = new Binding(string.Format("[{0}]", i));
    _dataGrid.Columns.Add(col);
}
this.ExternalData._dataGrid.ItemsSource = array;
string[][]数组=fs.CSVToStringArray();
对于(int i=0;i