1

我遇到了一个问题,即我的组合框在关闭 UserControl 时丢失了其 SelectedIndex 值。ViewModel 仍然有它,但视图不断将它重置为 -1。我了解绑定 ItemSource 和 SelectedIndex 的顺序存在问题,但我没有直接绑定到 ItemSource。基本上,我试图找出下面绑定的正确语法。

               </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>
4

1 回答 1

1

您是绑定到索引(int)还是项目(对象)。您的示例绑定到指示索引而不是对象的属性。

您应该设置 SelectedIndex 绑定的 Mode 属性

<ComboBox SelectedIndex="{Binding SelectedConnectionIndex, Mode=TwoWay}">
</ComboBox>
于 2012-05-24T15:29:49.543 回答