【问题标题】:Binding SelectedIndex to a different Collection then the Collection of the itemsSource将 SelectedIndex 绑定到不同的集合,然后是 itemsSource 的集合
【发布时间】:2013-03-20 10:58:20
【问题描述】:

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

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

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

基本上我需要的是能够将我的 Selectedindex 绑定到与我的组合框的集合不同的集合中的属性。我会给你一些 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>

顺便说一句,我不确定这是否可能。 谢谢

【问题讨论】:

  • 组合框是如何生成的?您可以将所选项目绑定到视图模型中的任何属性,并在任何函数中更改该值
  • 组合框绑定到一个 observablecollection,当我使用所选项目时,组合框在页面加载时保持空白

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


【解决方案1】:

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

即:

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

【讨论】:

    猜你喜欢
    • 2010-12-10
    • 2018-07-16
    • 2012-12-10
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 2023-03-07
    相关资源
    最近更新 更多