Binding to a DataGridComboBoxColumn from a collection

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

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, perhaps, but here is the 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 I really need this in a DataGridComboBoxColumn . Here are my changes:

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> 

FROM#:

Remains the same.

The problem now is that I get empty columns with a list! Any ideas how I can make this work?

Ultimately, I need to bind 2 paths, where double-clicking on the firstname column calls the comobo field, which then stores options for all possible first names in the collection (for example, George, Jim and Amy).

Thanks for the help!

+4
source share
2 answers

I just wanted to point you to one of the best links when it comes to the WPF Toolkit Datagrid, it comes from Samuel Moura I downloaded 15 samples and saved them, I give you a link because you will find the Datagrid series of messages more useful than any link than I can give you a quick response.

+1
source

The DataGrid must have the Header and ItemsSource parameters 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 seems that in one of the toolkit releases when using DataGridComboBoxColumn.ItemsSource : DataGridComboBoxColumn.ItemsSource does not work .

However, to use combined fields with WPF DataGrid . Finally, you can take a look at the article More fun with DataGrid by Margaret Parsons.

Edit
Now I'm not sure if the code above works. I made this from memory and referred to other links as resources.

Take a look at this SO entry, which seems to be intended to solve this problem: DataGridComboBoxColumn.ItemsSource binding problem

+1
source

Source: https://habr.com/ru/post/1300690/


All Articles