开发者

Binding to DataGridComboBoxColumn from collection

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 collectio开发者_JAVA技巧n (i.e. George, Jim and Amy).

Grateful any assistance!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜