DataGridTemplateComboboxColumn in WPF

Carlos Lino 6 Reputation points
2020-06-09T21:34:01.697+00:00

Hi all:

I'm new in WPF and I'm having a hard time trying to make the Combobox works in a DataGridTemplateColumn. I don't want to use DatagridComboboxColumn because the cells on this column look like a text column, the user has to click on it so the arrow down shows up.

I'm able to populate the Combobox but when I select a value from any row, it changes to all the rest of the cells in the Datagrid. What am I doing wrong? Hope you can help me, please.

Thanks in advance.

.cs

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {           
        LoadEmployee();
    }

public List<GenderList> GenderList { get; set; }

void LoadEmployee()
        {
            GenderList = new List<GenderList>()
            {
                new GenderList() { Codigo = 100, Descripcion = "MALE" },
                new GenderList() { Codigo = 200, Descripcion = "FEMALE"; }
            };

            Employees = new List<Employee>()
            {
                new Employee() {Codigo = 1, Name = "CARLOS", Gender = "MALE", Birthday = new DateTime(1971, 7, 23)}, 
            new Employee() {Codigo = 2, Name = "ANDREA", Gender = "FEMALE", Birthday = new DateTime(1971, 7, 23)},
            new Employee() {Codigo = 3, Name = "JOSE", Gender = "FEMALE", Birthday = new DateTime(1971, 7, 23)},
            new Employee() {Codigo = 4, Name = "CHICHI", Gender = "FEMALE", Birthday = new DateTime(1971, 7, 23)},
            new Employee() {Codigo = 5, Name = "MILU", Gender = "FEMALE", Birthday = new DateTime(1971, 7, 23)},
            new Employee() {Codigo = 6, Name = "MIA", Gender = "FEMALE", Birthday = new DateTime(1971, 7, 23)}
            };            
            dglist.ItemsSource = Employees;

            var cvs = (FindResource(&#34;StatusItems&#34;) as CollectionViewSource);
            cvs.Source = GenderList;
        }

public class Employee
        {
            public int Codigo { get; set; }
            public string Name { get; set; }
            public string Gender { get; set; }
            public DateTime Birthday { get; set; }
        }

.xaml

<Window.Resources>
        <CollectionViewSource x:Key="StatusItems"/>
 </Window.Resources>
.
.
.
<DataGridTemplateColumn Header="Combo">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox BorderThickness="0"
                                ItemsSource="{Binding Source={StaticResource StatusItems}}"
                                SelectedValue="{Binding GenderList, UpdateSourceTrigger=PropertyChanged}"
                                DisplayMemberPath="Descripcion"
                                SelectedValuePath="Codigo"
                                      />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,667 questions
0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. Carlos Lino 6 Reputation points
    2020-06-24T20:27:11.383+00:00

    Thank you for the demo, I really appreciate it. Last question. How to set a value on any cell and row of Datagrid?

    Datagrid seems to be more difficult as I expected. I'm asking for these things in the previous threads because the main goal is to create an empty Datagrid for data entry. As far as I know, Datagrid needs to be bound to a collection or a list and that makes a little harder to handle it. That's why my questions: How to get/set a value on any cell and row of Datagrid?

    0 comments No comments

  2. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-06-25T06:45:31.043+00:00

    Hi, the DataGrid is a UIElement that lets you view and change data from a buffer. The buffer is a list (table) with data objects. Each column is bound to a property of the data object. In addition, the user can navigate through the data objects. Displayed data are changed by changing the data in the buffer (data object). Then the UI has to be updated, e.g. via NotifyPropertyChanged.

    0 comments No comments