UserControl
안에 DataGrid
이 있습니다. 그것은 다음과 같습니다문자열 배열을 WPF DataGrid에 자동 바인딩하는 방법은 무엇입니까?
<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
을 사용하여 :
<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>
I는 다음과 같습니다 내 MainWindow
에서 UserControl
이 , CSV 파일을 열어 내용을 구문 분석하여 string[][]
외부 배열이 행과 내부 arr을 나타냅니다. 문자열 [0] [3] I는 단지 어레이의 배열과 동일 ItemsSource
설정 뒤에 내 코드에서 행 1 열 4
될 수 있도록 다음과 같이 바깥 열의 모든 위치 :
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)
{
}
}
}
출력은 내가 기대 한 바가 아니다. 나는 실제 내용 대신 객체의 속성을 보여주는 것으로 생각한다. 자동으로 행과 열을 생성하는 방법이 있습니까? 아니면 열을 만들고 행을 작성하는 코드를 작성해야합니까? 나는 DataGrid
으로 string[][]
또는 List<string[]>
과 같은 것을 ItemsSource
에 바인딩 할 수 있다고 생각했습니다. 그냥 열을 생성했을 것입니다. 현재 열을 자동으로 생성하고 있으며, 단지 string[][]
의 내용으로 채우지 않습니다.