Bruie
Bruie

Reputation: 1235

Selected Value Combo Box not binding

Ok so I have spent hours now trying to figure this out and I cant. I have the below combo box which is binding correctly to my collection of data.

<ComboBox Name="cbx" Width="250" Height="25"
   Visibility="{Binding Path=IsComboBox,Converter={StaticResource BoolConverter}}"
   ItemsSource="{Binding Path=Answers}"
   SelectedValuePath="AnswerId"
   SelectedItem="{Binding Path=SelectedAnswer, Mode=TwoWay}" 
   DisplayMemberPath="Answer"/>

The Selected Item however is not populating top my Selected Answer property. I put a textbox on the form and bound it to SelectedAnswer.Answer and that is binding to the answer correctly. For some reason though my combo box will not bind the selected answer

Please help with this.

SurveyAnswer:

public class SurveyAnswer : INotifyPropertyChanged
{
    private Guid answerId;
    public Guid AnswerId
    {
        get { return answerId; }
        set { 
            answerId = value;
            NotifyPropertyChanged("AnswerId");
        }
    }

    private string answer;
    public string Answer
    {
        get { return answer; }
        set { 
            answer = value;
            NotifyPropertyChanged("Answer");
        }
    }

    public Guid SurveyLineID { get; set; }

    private bool isSelected;
    public bool IsSelected
    {
        get { return isSelected; }
        set { 
            isSelected = value;
            NotifyPropertyChanged("IsSelected");
        }
    }


    #region NotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    #endregion        

}

Upvotes: 0

Views: 2217

Answers (1)

AlignedDev
AlignedDev

Reputation: 8232

I think you need to change SelectedItem to SelectedValue. Sometimes that order of parameters matters as well.

 <ComboBox Name="cbx" Width="250" Height="25"                                                          
   Visibility="{Binding Path=IsComboBox,Converter={StaticResource BoolConverter}}" 
   ItemsSource="{Binding Path=Answers}"         
   SelectedValue="{Binding Path=SelectedAnswer, Mode=TwoWay}" 
   DisplayMemberPath="Answer" SelectedValuePath="AnswerId"/>

This is helpful: http://johnpapa.net/binding-to-silverlight-combobox-and-using-selectedvalue-selectedvaluepath-and-displaymemberpath

Upvotes: 1

Related Questions