WPF Binding Combox to DataGrid Cell

Jatin Jain 1 Reputation point
2021-02-05T14:13:45.85+00:00

How to bind Combobox to some cells of DataGrid in WPF using MVVM pattern

Developer technologies Windows Presentation Foundation
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,646 Reputation points
    2021-02-08T01:38:58.15+00:00

    I will show you a demo of binding ComboBox to DataGrid Cell.

    Part1: Code for Model part

     public enum SexOpt { Male, Female };  
      
        public class Member  
        {  
            public string Name { get; set; }  
            public SexOpt Sex { get; set; }  
      
        }  
    

    Part2: Code for ViewModel part

     public class ViewModel  
        {  
            public ObservableCollection<Member> memberData { get; set; }  
            public ViewModel()  
            {  
                memberData = new ObservableCollection<Member>();  
                Random radom = new Random();  
      
                for (int i = 1; i < 10; i++)  
                {  
                    Member men = new Member();  
                    men.Name = "JOE" + i.ToString();  
                    if (i % 2 == 0)  
                    {  
                        men.Sex = SexOpt.Male;  
                    }  
                    else  
                    {  
                        men.Sex = SexOpt.Female;  
                    }  
                    memberData.Add(men);  
                }  
            }  
        }  
    

    Part3: Code for View part, add xmlns:assembly="clr-namespace:System;assembly=mscorlib" for xaml

     <Window.Resources>  
            <ObjectDataProvider x:Key="sexEnum" MethodName="GetValues" ObjectType="{x:Type assembly:Enum}">  
                <ObjectDataProvider.MethodParameters>  
                    <x:Type Type="local:SexOpt"/>  
                </ObjectDataProvider.MethodParameters>  
            </ObjectDataProvider>  
        </Window.Resources>  
        <Window.DataContext>  
            <local:ViewModel></local:ViewModel>  
        </Window.DataContext>  
        <Grid>  
            <DataGrid x:Name="dataGrid" ItemsSource="{Binding memberData}" AutoGenerateColumns="False"  SelectionMode="Extended" SelectionUnit="CellOrRowHeader" >  
                <DataGrid.Columns>  
                    <DataGridTextColumn Header="Name" Width="80" Binding="{Binding Name}"/>  
                    <DataGridComboBoxColumn Width="80" Header="Sex"   
                                            SelectedItemBinding="{Binding Sex}"   
                                            ItemsSource="{Binding Source={StaticResource sexEnum}}"/>  
                </DataGrid.Columns>  
            </DataGrid>  
        </Grid>  
    

    The result picture is:
    65072-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.