2012-05-24 47 views
1

我有一個問題,在關閉UserControl時,我的組合框丟失了它的SelectedIndex值。 ViewModel仍然有它,但視圖不斷地將其重置爲-1。我知道綁定ItemSource和SelectedIndex的順序有問題,但我沒有直接綁定到ItemSource。基本上,我試圖找出下面的綁定正確的語法。組合框丟失SelectedIndex

   </ComboBox.ItemTemplate> 
       <ComboBox.ItemsSource> 
        <CompositeCollection> 
         <ComboBoxItem IsEnabled="False">Select a database connection...</ComboBoxItem> 
         <CollectionContainer Collection="{Binding Source={StaticResource ConnectionsBridge}}" /> 
         <ComboBoxItem>&lt;New...&gt;</ComboBoxItem> 
        </CompositeCollection> 
       </ComboBox.ItemsSource> 

       **<ComboBox.SelectedIndex> 
        <Binding Path="SelectedConnectionIndex"/> 
       </ComboBox.SelectedIndex>** 

      </ComboBox> 
+0

原來,結合現在是正確的感謝Shawn的迴應,但該指數仍是重置模式屬性。 – nathantruant

回答

1

你綁定到索引(int)或項目(對象)。您的示例綁定到一個屬性,該屬性將指示索引而不是對象。

你應該設置selectedIndex結合

<ComboBox SelectedIndex="{Binding SelectedConnectionIndex, Mode=TwoWay}"> 
</ComboBox> 
+0

Ooops,我的意思是SelectedIndex。 – nathantruant

+0

我無法綁定到組合框級別的SelectedIndex,因爲我的ItemSource綁定正在CompositeCollection中發生。這是關鍵。我需要知道如何在標記內完成上面的操作,INSIDE嵌套的 nathantruant

相關問題