2013-04-22 1 views
0

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이 이전 요소를 새 값으로 덮어 쓰는 것과 같습니다. 나는 왜 그렇게 행동하는지 모르겠다.

Application Window

+0

'File.ReadAllLines (파일명) ALL 기타 사항 서보 -OFF (아래 참조) (X => x.Split ('')). 선택 (X => 새로운 모델 {NAME = X [0] = AsNumber int.Parse (x [1])) ' – Will

답변

0

당신은 한 번만 Model를 만들고, 그것을 마지막 데이터 읽기/w마다 덮어 쓰기를하고 있습니다. 당신이하고 싶은 것은 각 라인을 읽을 때마다 새로운 Model을 생성하는 것입니다. while 루프 내에서 Model 인스턴스를 옮길 수 있습니다.

// My data field 
private ObservableCollection<Model> _internalFile = new ObservableCollection<Model>(); 

// My data accessor/setter 
public ObservableCollection<Model> InternalFile { get { return _internalFile; } set { _internalFile = value; } } 

while (fileReader.Peek() != -1) 
    { 
    Model x = new Model(); 
    // words = read the line, split the line, make a list of columns 
    var words = fileReader.ReadLine().Split(',').ToList(); 

     if ((words[0] != "Name") && (!String.IsNullOrEmpty(words[0])) 
     { 
     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. 
     } 
    } 
+0

그걸 수정했습니다. 데이터가 그리드에 잘 나타납니다. 그러나 나는 그것이 어디에서 왔는지 모르는 바닥에 여분의 줄을 아직도 가지고있다. –

+0

나는 그것이 DataGrid의 CanUserAddRows 속성 일 것 같아요. CanUserAddRows = false로 설정해보십시오. –

+0

@KaranveerPlaha 또한 문서 끝에 줄 바꿈 (빈 줄)이 없는지 확인하십시오. 내 업데이트 –

관련 문제