A bug bound to itemssource of WPF itemscontrol

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.