Dharma February 2016

How can i Create a wpf Datagrid view with dynamically generating combobox columns

How can i Create a Datagrid view with dynamically generating combobox (through c#) columns and how can i bind the data to those dynamically generated columns (through MVVM). i want to generate different collection of data to each combobox inside the automatically generated column.

Thanks in advance.

Answers


silverfighter February 2016

Not sure why you want to add the combobox via c#. Why not use a Template Column where you take advantage of MVVM as you should do. View is defined in XAML and content is bound via ViewModel data.

<DataGridTemplateColumn Header="ColumnHeader">
     <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
                 <local:CostumControlWithCombobox Text="{Binding Path=YourListToPickFrom}"/>
           </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

If the pick lists should be possibly different for certain row. You need a rule or a parameter how to bind the matching list. Note that it is also possible to add controls with mvvm behavior in a TemplateColumn (just for the sake of demonstration) but you could also bind to a combobox list itself.

If you still prefer to build this up via C# code Check out the Class documentation of DataGridTemplateColumn they can also be accessed via code. https://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtemplatecolumn(v=vs.110).aspx

HTH

Post Status

Asked in February 2016
Viewed 2,064 times
Voted 11
Answered 1 times

Search




Leave an answer