I don't know whether it is a bug or something. I encountered this problem.
I tested it. All itemscontrols sub-classes have this problem. For example: ComboBox
Public class namevalue
{
Public object value {Get; set ;}
Public string name {Get; set ;}
}
Public class testobj: inotifypropertychanged
{
Public event propertychangedeventhandler propertychanged;
Protected void notify (string propname)
{
If (this. propertychanged! = NULL)
{
Propertychanged (this, new propertychangedeventargs (propname ));
}
}
Int _ V1;
Public int V1 {
Get {return _ V1 ;}
Set {
If (this. _ V1 = value) {return ;}
This. _ V1 = value;
Y ("V1 ");
}
}
Int _ V2;
Public int V2 {
Get {return _ V2 ;}
Set {
If (this. _ v2 = value) {return ;}
This. _ v2 = value;
Y ("V2 ");
}
}
}
Initialize list
List <namevalue> lst = new list <namevalue> ();
Lst. Add (New namevalue () {name = "Item1", value = "1 "});
Lst. Add (New namevalue () {name = "item2", value = "2 "});
Lst. Add (New namevalue () {name = "item3", value = "3 "});
Bind to ComboBox
Cbo1.itemssource = lst;
Cbo2.itemssource = lst;
DataGrid. datacontext = new testobj (){
V1 = 1,
V2 = 2
};
XAMLCode
<Grid X: Name = "DataGrid">
<ComboBox X: name = "cbo1" displaymemberpath = "name" selectedvaluepath = "value" width = "80" issynchronizedwithcurrentitem = "true" selectedvalue = "{binding Path = V1}"/>
<ComboBox X: name = "cbo2" displaymemberpath = "name" selectedvaluepath = "value" width = "80" issynchronizedwithcurrentitem = "true" selectedvalue = "{binding Path = v2}"/>
</GRID>
Test. The result is that cbo1 and cbo2 are linked.
Solution
Cbo1.itemssource = lst. toarray ();
Cbo2.itemssource = lst. toarray ();
DataGrid. datacontext = new testobj (){
V1 = 1,
V2 = 2
};
Itemssource points to the same list, causing select synchronization. The toarray () method actually clones two namevalue array instances, so that select synchronization is not generated.