4

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!

baron
  • 11,011
  • 20
  • 54
  • 88
  • I suspect from reading this: http://blogs.msdn.com/vinsibal/archive/2008/10/31/wpf-datagrid-datagridcomboboxcolumn-v1-intro.aspx is where the problem lays. I need to set SelectedItemBinding in the XAML, but since my list is defined in code, how do I do this? No I do not want to set it in XAML, because another part of this application allows adding people objects, so the list is ever changing. – baron Feb 08 '10 at 23:47

1 Answers1

1

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

Community
  • 1
  • 1
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
  • This doesn't work. I get the same as before, except obviously they now have headers. It is the correct number of rows, but blank, and double clicking brings up the comobobox, but this is also blank. – baron Feb 08 '10 at 23:56
  • See edit for reference to other SO post which should address the problem. – Metro Smurf Feb 09 '10 at 01:00
  • Can you help show me how I would adapt my problem to the solution in that SO question? Because I have set ItemsSource in code behind, he does it in xaml – baron Feb 09 '10 at 02:20