Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在数据网格列中显示ObservableCollection元素_Wpf_Data Binding_Mvvm_Datagrid_Observablecollection - Fatal编程技术网

Wpf 在数据网格列中显示ObservableCollection元素

Wpf 在数据网格列中显示ObservableCollection元素,wpf,data-binding,mvvm,datagrid,observablecollection,Wpf,Data Binding,Mvvm,Datagrid,Observablecollection,我有一个程序,我正在读取一个包含两列的.csv文件,我的模型有两个属性。 我在读取文件时获取该模型的实例,然后将它们添加到我的ObvervableCollection中 下面是它的外观: // My data field private ObservableCollection<Model> _internalFile = new ObservableCollection<Model>(); // My data accessor/setter public Obse

我有一个程序,我正在读取一个包含两列的.csv文件,我的模型有两个属性。 我在读取文件时获取该模型的实例,然后将它们添加到我的ObvervableCollection中

下面是它的外观:

// 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. 
    }
  }
//我的数据字段
私有ObservableCollection_internalFile=新ObservableCollection();
//我的数据存取器/设置器
公共ObservableCollection InternalFile{get{return{uInternalFile;}set{{uInternalFile=value;}}
型号x=新型号();
while(fileReader.Peek()!=-1)
{
//words=读这行,拆分这行,列出列
var words=fileReader.ReadLine().Split(',').ToList();
如果(单词[0]!=“名称”)
{
x、 asWord=words[0];
x、 asNumber=int.Parse(字[1]);
添加(x);
//InternalFile是“模型”对象的集合。
//因此创建了“模型”占位符x,并将x的每个实例堆积起来
//在要显示的集合中。
}
}
我的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正在用一个新值覆盖其以前的元素。我不知道为什么会这样


您只需创建一次
模型
,并在每次读取最后一次数据时将其覆盖。您要做的是为读取的每一行创建一个新的
模型
。您只需在while循环中移动
模型
实例化即可。(见下文)

//我的数据字段
私有ObservableCollection_internalFile=新ObservableCollection();
//我的数据存取器/设置器
公共ObservableCollection InternalFile{get{return{uInternalFile;}set{{uInternalFile=value;}}
while(fileReader.Peek()!=-1)
{
型号x=新型号();
//words=读这行,拆分这行,列出列
var words=fileReader.ReadLine().Split(',').ToList();
if((单词[0]!=“Name”)&(!String.IsNullOrEmpty(单词[0]))
{
x、 asWord=words[0];
x、 asNumber=int.Parse(字[1]);
添加(x);
//InternalFile是“模型”对象的集合。
//因此创建了“模型”占位符x,并将x的每个实例堆积起来
//在要显示的集合中。
}
}

这就解决了问题。数据在网格中显示得很好。但是,我确实在底部还有一行,我不知道它来自哪里。我相信这可能是DataGrid的CanUserAddRows属性。请尝试将其设置为CanUserAddRows=false@KaranveerPlaha另外,确保没有换行符(空行)在文档的末尾。请参阅我的更新,完成了。谢谢
File.ReadAllLines(文件名)。选择(x=>x.Split(','))。选择(x=>newmodel{Name=x[0],AsNumber=int.Parse(x[1])
// 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. 
      }
  }