WPF DataGrid binding ComboBox
One of the columns in the DataGrid in WPF is the ComboBox solves this problem for a lot of time, no nonsense about directly on the code
XAML Code
<datagridtemplatecolumn header= "group name" >
<DataGridTemplateColumn.CellTempLate>
<DataTemplate>
<combobox selectedvalue= "{Binding path=name}" itemsource={binding Path=selectionlist,relativesource
={relativesource Ancestortype={x:type usercontrol},mode=findancestor}}></combobox>
<DataTemplate>
<DataGridTemplateColumn.CellTempLate>
</datagridtemplatecolumn >
Xmal's code is finished. The following code is written in the background.
The. CS code is as follows:
Public observablecollection<string> SelectionList//observablecollection This is a class that needs to be referenced (using System.Collections.ObjectModel)
{
Get{return _selectionlist;}
Set{_selectionlist=value;}
}
Private observablecollection<string> _selectionlist=new observablecollection<string> (); This is an important step in the realization.
Why use ObservableCollection class, can search on the Internet by oneself
The next step should be to add a value to SelectionList.
SelectionList. ADD ("Name1");
SelectionList. ADD ("Name2");
So the ComboBox down the box has Name1 and Name2, of course SelectionList can also add background query Datatabel value, nothing but add a loop just
This is the value to add a value to the ComboBox, the last step, the value from the database to bind to the ComboBox, so that the default display
(select Name, field ... from table) query a collection of DataTable
The name of the DataGrid (its own name) Datagrid.itemsource=datatable.defaultview;
The queried name will be bound to the ComboBox.
WPF DataGrid Binding ComboBox