Before writing the text, I have to say that the other WPF on the web to bind the way the ComboBox, after watching really let people want to die.
Let's start by telling you a whole bunch of models, a bunch of control templates, and maybe you just want to know how to make the ComboBox show up.
Convention First:
To achieve this effect is very simple, except to let the data model tightly only a few lines of code.
Look at the data model first:
View Sourceprint?
01.
public
class
VModel : INotifyPropertyChanged
02.
{
03.
private
string _Name;
04.
05.
public
string Name
06.
{
07.
get {
return
_Name; }
08.
set
09.
{
10.
if
(_Name != value)
11.
_Name = value;
12.
OnPropertyChanged(
"Name"
);
13.
}
14.
}
15.
16.
private
List<string> _Desciption;
17.
18.
public
List<string> Desciption
19.
{
20.
get {
return
_Desciption; }
21.
set {
22.
if
(_Desciption != value)
23.
_Desciption = value;
24.
OnPropertyChanged(
"Desciption"
);
25.
}
26.
}
27.
28.
public
event PropertyChangedEventHandler PropertyChanged;
29.
30.
protected
void
OnPropertyChanged(string propertyName)
31.
{
32.
if
(string.IsNullOrEmpty(propertyName))
throw
new
ArgumentNullException(
"propertyName"
);
33.
PropertyChangedEventHandler handler = PropertyChanged;
34.
if
(handler !=
null
) handler(
this
,
new
PropertyChangedEventArgs(propertyName));
35.
}
36.
37.
}
The onpropertychanged in the back does not need to be cared for in order to achieve dynamic data changes, generally not required
Look at the template for the ComboBox in the DataGrid that's the point.
View Sourceprint?
01.
<DataGrid AutoGenerateColumns=
"False"
Height=
"236"
HorizontalAlignment=
"Left"
Margin=
"12,0,0,0"
Name=
"dataGrid1"
VerticalAlignment=
"Top"
Width=
"471"
>
02.
<DataGrid.Columns>
03.
<DataGridTextColumn Header=
"Header1"
Binding=
"{Binding Name}"
/>
04.
<DataGridTemplateColumn Header=
"Template"
>
05.
<DataGridTemplateColumn.CellTemplate>
06.
<DataTemplate>
07.
<ComboBox ItemsSource=
"{Binding Desciption}"
/>
08.
</DataTemplate>
09.
</DataGridTemplateColumn.CellTemplate>
10.
</DataGridTemplateColumn>
11.
</DataGrid.Columns>
12.
</DataGrid>
You see that? Total two columns A TextBox a ComboBox is simply a few lines apart from the data model can be done!
Initialization of the data:
View Sourceprint?
01.
List<VModel> vs =
new
List<VModel>();
02.
VModel v1 =
new
VModel();
03.
v1.Name =
"Sean"
;
04.
v1.Desciption =
new
List<string>();
05.
v1.Desciption.Add(
"1"
);
06.
v1.Desciption.Add(
"2"
);
07.
v1.Desciption.Add(
"3"
);
08.
vs.Add(v1);
09.
dataGrid1.ItemsSource = vs;
also attachedTo add a static entity to a resource and then reference the resource when binding, two steps are done:
Public observablecollection<department> Listdepartments{set;get;}
Listdepartments = DB. Getcollection<department> ();
This. DataContext = this;
<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"/>
A simple way to bind the ComboBox of a DataGrid in WPF (Absolutely simple)