0

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>

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes! That does the trick. I had seen that in a couple places, but the tutorials I was looking at used a List and didn't mention anything about that. Thx for the very quick reply :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.