Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying elements of ObservableCollection in data grid columns
    primarykey
    data
    text
    <p>I have a program where I am reading a .csv file with 2 columns, and my model has 2 properties. I take instances of this model as the file is read and then add them to my ObvervableCollection.</p> <p>Here is what it looks like:</p> <pre><code>// My data field private ObservableCollection&lt;Model&gt; _internalFile = new ObservableCollection&lt;Model&gt;(); // My data accessor/setter public ObservableCollection&lt;Model&gt; 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. } } </code></pre> <p>My XAML looks like this </p> <pre><code>&lt;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"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;DataGrid Grid.Row="0" AutoGenerateColumns="False" CanUserReorderColumns="False" ItemsSource="{Binding Path=InternalFile}"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="As Word" IsReadOnly="True" Width="Auto" Binding="{Binding asWord}" /&gt; &lt;DataGridTextColumn Header="As Number" IsReadOnly="True" Width="Auto" Binding="{Binding asNumber}" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;Label Grid.Row="1" Content="{Binding Status}" /&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>In the window displayed, I am seeing the columns populate but all the rows are the last row that was read before end of file. It is as if the ObservableCollection is overwriting its previous elements with a new value. I don't know why it's behaving like that.</p> <p><img src="https://i.stack.imgur.com/2B8hO.png" alt="Application Window"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload