The selectedvalue of Silverlight ComboBox does not fail to be bound to itemsource.

Source: Internet
Author: User

Simple mvvm is being practiced. Use ComboBox to select and bind data from the drop-down menu, and then you will encounter problems.

There are three items in the ListBox on the left, and only two items in ComboBox. On the left, select bind to the right.

123

1. All the data items that Wang and Xu have been elected are normal.

2. When Xie is selected, there should be no selected items in ComboBox because this option is not available. This is understandable.

3. But when you select a common data item again, the choice in ComboBox cannot be bound.

After the endless Google, Maybe Google's technology is poor and there is no good solution. You can only do it yourself.

First, define a control to inherit from ComboBox.

Public class novaluecombobox: ComboBox

Find the event with an error when binding. If no event is found, you can only register the event with the selected item change.

Code

Public novaluecombobox ()
{
This. selectionchanged + = new selectionchangedeventhandler (nullablecombobox_selectionchanged );

}

Void nullablecombobox_selectionchanged (Object sender, selectionchangedeventargs E)
{

}

Observe E and find that E. addeditems. Count = 0 when no item is selected. It may be controlled at this time.

After several attempts, it is suspected that the binding is faulty. I think we may be able to continue using it when we rebind it.

First, we suspect itemsource.

Code

If (E. addeditems = NULL | E. addeditems. Count = 0)
{
VaR be = This. getbindingexpression (novaluecombobox. itemssourceproperty );
VaR B = be. parentbinding;
This. setbinding (novaluecombobox. itemssourceproperty, B );
}

Useless. Selectedvalue is also suspected.

Code

Void nullablecombobox_selectionchanged (Object sender, selectionchangedeventargs E)
{
If (E. addeditems = NULL | E. addeditems. Count = 0)
{
VaR be = This. getbindingexpression (novaluecombobox. itemssourceproperty );
VaR B = be. parentbinding;
This. setbinding (novaluecombobox. itemssourceproperty, B );
Be = This. getbindingexpression (novaluecombobox. selectedvalueproperty );
B = be. parentbinding;
This. setbinding (novaluecombobox. selectedvalueproperty, B );
}
}

At this time, an amazing error was reported. When getting the binding value of selectedvalueproperty, null is obtained. It turns out that selectedvalue has not been bound at this time, so it is no wonder that the subsequent data cannot be bound. Whether the binding parameter can be obtained when the control is loaded, that is, when the selectedvalue and the bound value exist in the global variable, and then assigned a value to the selectedvalue when appropriate. The Code obtained after multiple attempts is as follows:

Code

Public class novaluecombobox: ComboBox
{
Binding _ binding;

Public novaluecombobox ()
{
This. selectionchanged + = new selectionchangedeventhandler (nullablecombobox_selectionchanged );
}

Public override void onapplytemplate ()
{
Base. onapplytemplate ();

VaR be = This. getbindingexpression (novaluecombobox. selectedvalueproperty );
_ Binding = be. parentbinding;
}

Void nullablecombobox_selectionchanged (Object sender, selectionchangedeventargs E)
{
If (E. addeditems = NULL | E. addeditems. Count = 0)
{
This. setbinding (novaluecombobox. selectedvalueproperty, _ binding );
}
}
}


Check the effect.

123

The following ComboBox is customized and looks okay.

There may be better solutions. Please give us some advice.

Code: Demo

<ComboBox Height = "20" width = "60"

Displaymemberpath = "name"

Selectedvaluepath = "Age"

Selectedvalue = "{binding age, mode = twoway }"

Itemssource = "{binding persons}"/>

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.