2012-01-05 56 views
0

好吧,所以我花了幾個小時,試圖找出這一點,我不能。 我有下面的組合框,它正確地綁定到我的數據收集。選定的價值組合框沒有約束力

<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   

} 
+0

你能後如何填充了'Answers'收集和'SelectedAnswer'財產,也是'SelectedAnswer'財產的執行? – nemesv 2012-01-05 15:56:41

回答

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

+0

試圖改變我的組合框到這一點,它仍然沒有綁定選定的值 – Bruie 2012-01-05 14:30:44

+0

如果你在調試模式下運行,輸出窗口是否有任何綁定錯誤?更好的是,如果你在SL5中運行,你可以放入一個斷點並進行調試。你能發佈你的Answer對象的樣子嗎? – Aligned 2012-01-05 14:54:05

+0

發佈SelectedAnswer對象 - 簽入輸出窗口並且沒有綁定問題。如果這是Answers對象的問題,它將無法綁定到文本框 – Bruie 2012-01-05 15:09:56