In the Delphi era, I do not like to edit the data directly on the Stringgrid, and if the entire ComboBox in this Drop-down selection box, it is simply troublesome.
After using WPF, I think it should be edited directly on the Stringgrid, otherwise, you will have to drag out a lot of TextBox and ComboBox out, then the DataGrid is left only the selected role.
The text is very say, ComboBox on the trouble, how a trouble law? Binding up the data is useless!
1. Create a new list as the data source and set the window's Datacontex to this.
The code is as follows |
Copy Code |
Public observablecollection<department> Listdepartments{set;get;} Listdepartments = DB. Getcollection<department> (); This. DataContext = this; |
2, the DataGrid directly bound listdepartments
The code is as follows |
Copy Code |
<datagridcomboboxcolumn width= "header=" professional group selectedvaluepath= "Name" textbinding= "{Binding Department}" Itemssource= ' {Binding path=listdepartments} ' displaymemberpath= ' Name '/>
|
The above approach, is the general control of the data binding method, but put it in here is not the spirit!
Then hurry to find a solution, online things a lot, but do not solve the problem, probably said:
1, ComboBox in the DataGrid there is no Datacontex, so can not bind to the TreeView.
2, ComboBox can only bind static resources in the DataGrid.
3, and said Anccerster wrong.
Go to Microsoft MSDN for a look:
To populate the Drop-down list with one of the following options, set the ComboBox ItemsSource property First:
Static resources. For more information, see StaticResource Markup extensions.
X: Static code entity. For more information, see x:static Markup extensions.
An inline collection of comboboxitem types.
Damn, the requirements are really high, only the following three situations can be: static resources, static code entities, and one is to put your list data into a comboboxitem set.
Well, that's the easiest way, the second:
To add a static entity to the resource, and then binding the resource, two steps are done:
The code is as follows |
Copy Code |
<Window.Resources>
<collectionviewsource x:key= "departments" source= "{Binding listdepartments}"/> </Window.Resources> <datagridcomboboxcolumn width= "header=" professional group selectedvaluepath= "Name" textbinding= "{Binding Department}" Itemssource= ' {Binding source={staticresource departments} ' displaymemberpath= ' Name '/> |
Oh, it's hard.