2 열로 .csv 파일을 읽고 프로그램에 2 개의 속성이있는 프로그램이 있습니다. 파일을 읽을 때이 모델의 인스턴스를 가져 와서 ObvervableCollection에 추가합니다.ObservableCollection의 요소를 데이터 격자 열에 표시
// My data field
private ObservableCollection<Model> _internalFile = new ObservableCollection<Model>();
// My data accessor/setter
public ObservableCollection<Model> InternalFile { get { return _internalFile; } set { _internalFile = value; } }
Model x = new Model();
while (fileReader.Peek() != -1)
{
// words = read the line, split the line, make a list of columns
var words = fileReader.ReadLine().Split(',').ToList();
if (words[0] != "Name")
{
x.asWord = words[0];
x.asNumber = int.Parse(words[1]);
InternalFile.Add(x);
// InternalFile is a collection of 'Model' objects.
// So created 'Model' placeholder , x, and pile each instance of x
// in the collection to be displayed.
}
}
내 XAML, 나는 열을 채울보고하고 표시되는 창에서이
<Window x:Class="WpfMVVP.WindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfMVVP"
Title="Window View" Height="350" Width="525" Background="White">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" AutoGenerateColumns="False" CanUserReorderColumns="False" ItemsSource="{Binding Path=InternalFile}">
<DataGrid.Columns>
<DataGridTextColumn Header="As Word" IsReadOnly="True" Width="Auto" Binding="{Binding asWord}" />
<DataGridTextColumn Header="As Number" IsReadOnly="True" Width="Auto" Binding="{Binding asNumber}" />
</DataGrid.Columns>
</DataGrid>
<Label Grid.Row="1" Content="{Binding Status}" />
</Grid>
처럼 보이지만 모든 행은 다음과 같습니다 여기
는 모습입니다 파일의 끝 전에 읽힌 마지막 행. 마치 ObservableCollection이 이전 요소를 새 값으로 덮어 쓰는 것과 같습니다. 나는 왜 그렇게 행동하는지 모르겠다.
'File.ReadAllLines (파일명) ALL 기타 사항 서보 -OFF (아래 참조) (X => x.Split ('')). 선택 (X => 새로운 모델 {NAME = X [0] = AsNumber int.Parse (x [1])) ' – Will