选定值组合框不具有约束力

时间:2012-01-05 13:03:00

标签: silverlight xaml

好的,所以我花了好几个小时试图解决这个问题,我不能。 我有下面的组合框,它正确绑定到我的数据集合。

<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"/>

但是,所选项目不会填充我的选定答案属性。我在表单上放了一个文本框,并将其绑定到SelectedAnswer.Answer,并正确绑定到答案。 出于某种原因,虽然我的组合框不会绑定选定的答案

  • 我已经阅读了一些关于组合框属性的布局并尝试更改它的内容,同时逐步执行属性的getter和setter以确保它没有清除(这不是因为它将绑定到文本框)

请帮忙解决这个问题。

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        

}

1 个答案:

答案 0 :(得分:1)

我认为您需要将SelectedItem更改为SelectedValue。有时参数的顺序也很重要。

 <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"/>

这很有帮助: http://johnpapa.net/binding-to-silverlight-combobox-and-using-selectedvalue-selectedvaluepath-and-displaymemberpath