【问题标题】:Displaying elements of ObservableCollection in data grid columns在数据网格列中显示 ObservableCollection 的元素
【发布时间】:2013-04-15 15:24:45
【问题描述】:

我有一个程序,我正在读取一个包含 2 列的 .csv 文件,并且我的模型有 2 个属性。 我在读取文件时获取此模型的实例,然后将它们添加到我的 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. 
    }
  }

我的 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(filename).Select(x =&gt; x.Split(',')).Select(x =&gt; new Model {Name = x[0], AsNumber = int.Parse(x[1]))

标签: wpf data-binding mvvm datagrid observablecollection


【解决方案1】:

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

// 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. 
      }
  }

【讨论】:

  • 修复了它。数据很好地显示在网格中。但是,我确实在底部还有一行额外的行,我不知道它是从哪里来的。
  • 我相信这可能是 DataGrid 的 CanUserAddRows 属性。尝试将其设置为 CanUserAddRows = false
  • @KaranveerPlaha 另外,请确保文档末尾没有换行符(空行)。查看我的更新
  • 是的,做到了。谢谢
猜你喜欢
  • 2021-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-25
  • 1970-01-01
  • 2020-12-15
  • 1970-01-01
  • 2013-03-08
相关资源
最近更新 更多