2010-10-19 73 views
2

在我解釋我的問題,請考慮以下對象:Silverlight的雙向綁定的組合框的設定值設爲NULL

Character.cs 

-> AnimationControlSettings.cs 

.. -> UpControlType (string) 

.. -> AvailableControlTypes (List<string>) 

在我的視圖模型的相關屬性:

​​

我有一個簡單的視圖,其中您使用ComboBox選擇一個字符。 ComboBox的SelectedItem是雙向綁定到ViewModel的SelectedCharacter屬性的。還有其他文本框/複選框(也是雙向綁定到SelectedCharacter的各種屬性),在我在字符之間切換時可以正確保持它們的值。

<ComboBox x:Name="lstUpControlTypes" 
     ItemsSource="{Binding Path=SelectedCharacter.AnimationControlSettings.AvailableControlTypes}" 
     SelectedItem="{Binding Path=SelectedCharacter.AnimationControlSettings.UpControlType, Mode=TwoWay}"> 
</ComboBox> 

初始值正確地顯示在ComboBox的,但只要我從CharacterA切換到CharacterB,CharacterA的UpControl屬性設置爲NULL和:在綁定到UpControlType屬性的組合框存在

問題我不知道爲什麼。

下面是這種確切的問題(VS2010,SL4)準系統攝製: http://www.checksumlabs.com/source/TwoWayBindingWorkshop.zip

如果您運行的解決方案,您將看到Name屬性仍然存在,如您切換字符但UpControlType值的越來越設爲空值。

我在這裏錯過了一些明顯的東西嗎?

回答

1

你的第三組合框的項目源綁定的屬性SelectedCharacter裏面,像這樣:

ItemsSource="{Binding SelectedCharacter.AnimationControlSettings.AvailableControlTypes}" 

這意味着,當SelectedCharacter改變該組合框的項目源將被重置,這激活雙向綁定,在同一個組合框的的SelectedItem設置,你的屬性設置爲null:

SelectedItem="{Binding SelectedCharacter.AnimationControlSettings.UpControlType, Mode=TwoWay}" 

我能夠通過移動屬性AvailableControlTypes到CharacterViewModel類,這意味着WH來解決問題如果您更改字符,可用類型保持不變。如果在你的情況下這是可以接受的,它會解決你的問題:

  <ComboBox x:Name="lstUpControlTypes" 
         ItemsSource="{Binding AvailableControlTypes}" 
         SelectedItem="{Binding  SelectedCharacter.AnimationControlSettings.UpControlType, Mode=TwoWay}" /> 
+0

謝謝Murven,這很有意義。 – 2010-10-20 17:15:30