Nerf Herder
Nerf Herder

Reputation: 414

Cannot add rows to DataGrid after initialization

I am rather new to WPF (Visual Studio Express 2012), and like much of it, it's cool but it's not coming as easily as I would expect. Thanks to stackoverflow and examples and tutorials I'm picking it up, but on this I'm stymied.

I have a datagrid, I bind it to a list, and I expect that when I add something to the list, it shows up in the datagrid. That happens in the MainWindow function, but doesn't happen in my code to handle a button click (it used to work just fine when I had a ListBox, but a ListBox doesn't support checkboxes, at least not natively, so I want to convert it).

I'm wondering if a side note in this tutorial is important - it says the ItemSource refers to the original list, but the Items property is a converted ItemCollection. Stepping thru the code, I can see MyList gets the new items when I click the button, but it just doesn't show up in the UI.

Please help!

DataGridClass.cs:

namespace WpfTest
{
    class DataGridClass
    {
        public bool CheckboxColumn { get; set; }
        public string Text1Column  { get; set; }
        public string Text2Column  { get; set; }
    }
}

MainWindow.xaml.cs:

namespace WpfTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        List<DataGridClass> myList = new List<DataGridClass>();

        public MainWindow()
        {
            InitializeComponent();
            MyDataGrid.ItemsSource = myList;

            // this works
            myList.Add(new DataGridClass()  
            {
                CheckboxColumn = false,
                Text1Column = "Initialization",
                Text2Column = "ABCD"
            });

        }

        private void MyButton_Click(object sender, RoutedEventArgs e)
        {
            // this doesn't work
            myList.Add(new DataGridClass()
            {
                CheckboxColumn = false,
                Text1Column = "Button Clicked",
                Text2Column = "1234"
            });
        }
    }
}

MainWindow.xaml:

<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="MyButton" Content="Populate Chart" HorizontalAlignment="Left" Margin="75,36,0,0" VerticalAlignment="Top" Width="120" Click="MyButton_Click"/>
        <DataGrid x:Name="MyDataGrid" HorizontalAlignment="Left" Margin="75,76,0,0" VerticalAlignment="Top" Height="151" Width="349"/>

    </Grid>
</Window>

Upvotes: 0

Views: 230

Answers (1)

dkozl
dkozl

Reputation: 33384

You'll need to use ObservableCollection instead of List

ObservableCollection<DataGridClass> myList = new ObservableCollection<DataGridClass>();

it implements INotifyCollectionChanged interface which allows UI to pick up changes made to collection

Upvotes: 1

Related Questions