简体   繁体   中英

silverlight 4 combox not binding c#

I have a silverlight page that I bind a object to.

When I first load the page all the combo boxes fill and they show the correct selected item. When I refresh the page the combo boxes still have their items but the selected value doesn't bind.

Bound using

SelectedValue="{Binding WriterID,Mode=TwoWay}"
Article.DataContext = ActiveArticle

Update: It appears to be losing the databinding when I change the datacontext of the control Any ideas why?

As I indicated in my comment this is a known problem. I haven't been able to find the reference I was looking for, but I found this bug report on Microsoft Connect

The solution is to reset the binding expression when the selection changes. The code in the report does this in a subclassed ComboBox but if you are unable to override the ComboBox in your application you can do it in the view class.

public class XComboBox : ComboBox 
{ 
    private BindingExpression bE; 
    public XComboBox() 
    { 
        this.SelectionChanged += new SelectionChangedEventHandler(XComboBox_SelectionChanged); 
    } 

    void XComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
        if (bE==null) 
        { 
         bE = this.GetBindingExpression(ComboBox.SelectedValueProperty); 
        } 
        else 
        { 
            if (this.GetBindingExpression(ComboBox.SelectedValueProperty) == null) 
            { 
             this.SetBinding(ComboBox.SelectedValueProperty, bE.ParentBinding);     
            } 
        } 
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM