2012-05-24 49 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

相关问题