Search code examples
c#wpfwpfdatagrid

Why DataGrid adds empy elements on DataGrid.Items.Add?


In WPF i need, then i click button, i`m adding new row with information to DataGrid. But than I hit the buttin, new rows a creating, but its fill emlpty, not with any string. How to fix it? Thanks for advance.

XAML code:

<DataGrid Name="ClicksGrid" HorizontalAlignment="Left" Height="204" Margin="348,20,0,0" AutoGenerateColumns="False" CanUserAddRows="True" VerticalAlignment="Top" Width="354">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" IsReadOnly="True" Binding="{Binding Path=ID}" Width="40" />
                <DataGridTextColumn Header="ClickType" IsReadOnly="True" Binding="{Binding Path=Type}" Width="120" />
                <DataGridTextColumn Header="Time" IsReadOnly="True" Binding="{Binding Path=Time}" Width="193"/>
            </DataGrid.Columns>
        </DataGrid>

c# code:

// click class 
public class Click
    {
        public string ID;
        public string Type;
        public string Time;

        public Click(string ID, string Type, string Time)
        {
            this.ID = ID;
            this.Type = Type;
            this.Time = Time;
        }
    }


// by button clicking calling this method:

 private void TableUpdate()
        {
            IDnum++;

            //test strings:
            var cl = new Click("asd", "sdad", "asds");


            ClicksGrid.Items.Add(cl);   
        }

Solution

  • Change Click class in this way:

        public class Click
        {
            public string ID { get; set; }
            public string Type { get; set; }
            public string Time { get; set; }
    
            public Click(string ID, string Type, string Time)
            {
                this.ID = ID;
                this.Type = Type;
                this.Time = Time;
            }
        }