wpf - Add a bound ComboBox column to a DataGridColumn at runtime -


I would like to display a datagrid which will have data uploaded to the data base by data data, columns will be different each time, and some have a combo The box will need to be used.

How do I know about setting a DataGrid template column for a column, which requires a combo box at runtime?

Well, it has been found with the help of the closest m @Meleak, almost there, when the grid is not being edited, then display only the key instead of the price.

Public Partial Class MainWindow: Window {Public Dictionary MyDictionary {get; Set; }

  Public Manvindo () {InitializeComponent (); // Init Dictionary this.MyDictionary = New Dictionary & lt; Int, string & gt; (); this. MyDictionary.Add (1, "value1"); this. MyDictionary.Add (2, "value2"); this. MyDictionary.Add (3, "value 3"); Datatable dt = new datatable (); Data column column = new data column ("MyTipID", typef (int)); Dt.Columns.Add (column); Datarao Newsrao = DT. NRO (); NewRow ["MyTypeId"] = 1; Dt.Rows.Add (newRow); DataGrid.Column.Add (GetNewComboBoxColumn ("My Type", "MyTypeId", this. MyDictionary)); This.DataContext = dt; } Public Static Data Grid Template Column GetNewComboBoxColumn (String Header, String Binding Path, Object Items Source) {DataGridTemplateColumn comboBoxColumn = New DataGridTemplateColumn (); ComboBoxColumn.Header = header; Binding text binding = new binding (); TextBinding.Path = new property path (binding path); Framework Elementfactor TextBlock = New Framework Element Factor (Typograph (Textbook)); TextBlock.SetValue (TextBlock.MarginProperty, New Thickness (3, 3, 3, 3)); TextBlock.SetBinding (text blocking, text-response, text-padding); Framework Elementfactor Combo Box = New Framework Elementifier (typef (combo box)); ComboBox.SetValue (ComboBox.MarginProperty, New Thickness (1, 1, 1, 1)); ComboBox.SetBinding (ComboBox.TextProperty, text binding); ComboBox.SetValue (ComboBox.SelectedValuePathProperty, "key"); ComboBox.SetValue (ComboBox.DisplayMemberPathProperty, "Value"); Binding objects SourceBinding = new binding (); Itemourcebacing Source = item source; ComboBox.SetBinding (ComboBox.ItemsSourceProperty, ItemSourcing); ComboBoxColumn.CellTemplate = New DataTemplate (); ComboBoxColumn.CellTemplate.VisualTree = textBlock; ComboBoxColumn.CellEditingTemplate = New DataTemplate (); ComboBoxColumn.CellEditingTemplate.VisualTree = comboBox; Return comboBoxColumn; }             

update
You can use a DataGridComboBoxColumn and if you can set SelectedValueBinding to work it the desired way, just change this method to change it and This should work

  Public DataGrid column GetNewComboBoxColumn (string header, string binding path, object items source) {DataGridComboBoxColumn comboBoxColumn = New DataGridComboBoxColumn (); ComboBoxColumn.Header = header; ComboBoxColumn.SelectedValuePath = "Key"; ComboBoxColumn.DisplayMemberPath = "Value"; Binding binding = new binding (); Binding.path = new property path (binding path); ComboBoxColumn.SelectedValueBinding = Binding; Binding objects SourceBinding = new binding (); Itemourcebacing Source = item source; Binding operation .Setbinding (combo box column, datagrid combo box column. ITMSSourceProperty, itemSourceBinding); Return comboBoxColumn; }    

Comments