将SelectedIndex绑定到不同的Collection然后将itemsSource的Collection绑定

时间:2013-03-20 10:58:20

标签: c# listview combobox mvvm-light winrt-xaml

您好我正在使用MVVM-light开发WinRT应用程序。我想要做的是用一个项目填充一个组合框,例如4种水果。而我的SelectedItem应该是组合框结合的第一种水果。这有效,但下一步是我要进行selectedIndex更改。例如,当第一种水果售罄时,SelectedIndex应自动更改为Collection of my Combobox中的第二项。

因此,如果您想购买10片水果,您可以在屏幕上获得10个组合框,您可以在其中选择水果种类,当您在其中4个中选择苹果并且不再有苹果时,其他组合框的选择指数应改为第二种水果。

我知道这是一个令人困惑的问题。

基本上我需要的是能够将我的Selectedindex绑定到Collection中的属性,该属性与我的组合框的集合不同。我会给你一些xaml,希望能解释一下:

 </ListView>
   <ListView.ItemTemplate>
      <DataTemplate>
          <StackPanel IsHitTestVisible="{Binding HitTest}"  Orientation="Horizontal">
               <TextBlock Text="{Binding Day, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:dd MMM yyyy}' }"></TextBlock>
               <ComboBox Width="196"  ItemsSource="{Binding Path=FruitKind.FruitList, Source={StaticResource Locator}}" SelectedIndex="{Binding Path=FruitKind.FruitComboBoxItem, Source={StaticResource Locator}, ElementName={Binding Path=FruitIndex}}">
                  <ComboBox.ItemTemplate>
                     <DataTemplate>
                        <TextBlock Text="{Binding FruitKind}"/>
                     </DataTemplate>
                  </ComboBox.ItemTemplate>
              </ComboBox>
          </StackPanel>
     </DataTemplate>
   </ListView.ItemTemplate>
 </ListView>

我不确定这是否可行。 日Thnx

1 个答案:

答案 0 :(得分:0)

绑定到所选项目而不是索引。这允许您使用viewmodel的对象/属性,您可以以任何方式进行修改:

即:

<ComboBox Width="196"  ItemsSource="{Binding Path=FruitKind.FruitList, Source={StaticResource Locator}}" SelectedItem="{Binding Path=FruitKind.FruitObject, Source={StaticResource Locator}, ElementName={Binding Path=FruitIndex}}">