2012-06-14 59 views
0

我的組合框有綁定更新問題。我的ComboBox的ItemSource綁定到一個LaneConfigurations列表。 ComboBox的SelectedItem綁定到我的代碼隱藏中的SelectedLaneConfiguration屬性。我配置了ComboBox的ItemTemplate以顯示每個LaneConfiguration的'DisplayID'屬性。綁定在ComboBox中不更新當底層ComboBox項目描述發生變化時的文本字段

這在大多數情況下都適用。但是,更改車道配置可能會導致DisplayID發生變化。如果您選擇了特定車道並且DisplayID顯示爲組合框的「文本」,然後更改了LaneConfiguration對象,則組合框的「文本」部分不會更新爲應顯示的新「DisplayID」 。當我單擊組合框上的下拉箭頭時,出現在列表中的選定項目的項目顯示正確的DisplayID,但與'Text'中顯示在組合框頂部的'DisplayID'不匹配'字段。

在我的代碼背後,我在'SelectedLaneConfiguration'屬性上觸發PropertyChanged事件。如何讓ComboBox認識到'DisplayID'屬性需要更新?我試圖爲'DisplayID'屬性觸發PropertyChanged事件,但它是LaneConfiguration類的一部分,所以它似乎沒有更新。

我已經在下面包含了XAML以及顯示ComboBox文本顯示與ComboBox下拉內容不同步的屏幕截圖。

ScreenShot

部分XAML:

<ComboBox x:Name="_comboBoxLanes" Height="26" 
      ItemsSource="{Binding SensorConfiguration.LaneConfigurations}" 
      SelectedItem="{Binding Path=SelectedLaneConfiguration}"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding DisplayID, Mode=OneWay}"/> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

一些後臺代碼:

public partial class LaneManagement : INotifyPropertyChanged, IDisposable 
    { 
     .. 
     .. 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void FirePropertyChanged(string prop) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(prop)); 
      } 
     } 

     private void SensorConfiguration_Changed() 
     { 
      LaneTools.ResetLanePolygons(); 
      GenerateLaneTypeDropdowns(); 
      FirePropertyChanged("SensorConfiguration"); 
      FirePropertyChanged("SelectedLaneConfiguration"); 
      FirePropertyChanged("DisplayID"); 
     } 
    } 


    public class LaneConfiguration : PolygonConfiguration 
    { 
     public override string DisplayID 
     { 
      get 
      { 
       return IsLaneGroup?string.Format("Lanes {0} - {1}", ID, ID + LaneCount - 1):         string.Format("Lane {0}", ID); 
      } 
     } 
    } 
+0

看到一些C#代碼也很有幫助。 – Tim

+1

LaneConfigurations必須繼承INotifyPropertyChanged,你做到了嗎? –

+0

顯示「改變LaneConfiguration對象」的含義 – Paparazzi

回答

0

我創造的這個較短的例子,轉貼,就有了一個answer

我以爲我只需要在代碼隱藏的INotifyPropertyChanged。我實際上也需要它在我的數據對象LaneConfiguration類中。我應該更加關注阿里阿德爾的評論。這將節省我一天的挫折..

謝謝阿里阿德爾和蒂姆爲你有用的答案!

0

正確的方法是使您綁定到的屬性 - DependencyProperty - 比您不會遇到任何自動更新問題。

如果您使用INotifyPropertyCahgnge比它必須工作,如果它不 - 檢查PropertyChanged事件== null。

相關問題