Question

Trying to bind to a collection in WPF, I got the following to work:

XAML:

<toolkit:DataGrid Name="dgPeoples"/>

CS:

namespace DataGrid
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1
    {
        private readonly ObservableCollection<Person> personList = new ObservableCollection<Person>();

        public Window1()
        {
            InitializeComponent();

            personList.Add(new Person("George", "Jung"));
            personList.Add(new Person("Jim", "Jefferson"));
            personList.Add(new Person("Amy", "Smith"));

            dgPeoples.ItemsSource = personList;
        }
    }
}

unnessecary probably but here is Person class:

namespace DataGrid
{
    public class Person
    {
        public string fName { get; set; }
        public string lName { get; set; }

        public Person(string firstName, string lastName)
        {
            fName = firstName;
            lName = lastName;
        }
    }
}

But what I really need is this in DataGridComboBoxColumn 's. Here are my revisions:

XAML:

<toolkit:DataGrid Name="dgPeoples" Grid.Row="0" AutoGenerateColumns="False">
    <toolkit:DataGrid.Columns>
        <toolkit:DataGridComboBoxColumn Width="5*"/>
        <toolkit:DataGridComboBoxColumn Width="5*"/>
    </toolkit:DataGrid.Columns>
</toolkit:DataGrid>

C#:

Stays same.

Problem now is that I get empty combobox columns! Any ideas how I can get this to work?

In the long run I need 2 way binding, where a double click on the firstname column brings up the comobo box which then holds the options of all possible first names in the collection (i.e. George, Jim and Amy).

Grateful any assistance!

Was it helpful?

Solution

I just wanted to point you to one of the best reference when it comes to the WPF Toolkit Datagrid, it comes from from Samuel Moura I downloaded the 15 samples and keep them handy,I'm giving you the link because you'll find the Datagrid post series more useful than any reference than I can give you as quick answer

OTHER TIPS

The DataGrid needs to have the Header and ItemsSource Properties set:

<toolkit:DataGrid Name="dgPeoples" Grid.Row="0" AutoGenerateColumns="False">
    <toolkit:DataGrid.Columns>
        <toolkit:DataGridComboBoxColumn Width="5*"
            Header="First Name"
            ItemsSource="{Binding Path=fName}"/>
        <toolkit:DataGridComboBoxColumn Width="5*"
            Header="First Name"
            ItemsSource="{Binding Path=lName}"/>
    </toolkit:DataGrid.Columns>
</toolkit:DataGrid>

It appears there was in issue in one of the releases of the toolkit when using the DataGridComboBoxColumn.ItemsSource: DataGridComboBoxColumn.ItemsSource doesn't work.

However, there was a work-around created for Using combo boxes with the WPF DataGrid. Finally, you may want to take a look at the article More fun with DataGrid by Margaret Parsons as well.

Edit
Now I'm not so sure the above code works. I did that from memory and referenced the other links as resources.

Take a look at this SO post which appears to address this problem: Problem binding DataGridComboBoxColumn.ItemsSource

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top